I’m moving my player which is a ball moving with a joystick with a script like that :
_rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +(transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements));
but when I’m parenting the ball with a platform, the ball follow the platform like I want but I can’t move the ball on the plateform when it’s moving.
I don’t know what to do to fix that.
Child objects follow their parent. The behavior you’re describing is to be expected. Is there any particular reason why you’re making the player a child of the platform?
In the script below, I have implemented binding an object to another. An object with this script will behave the same as if it were a child of the target. But at the same time, the object with this script can move (fall, walk, etc.). [The script seems cumbersome, but this is due to the fact that it has more than 30 lines of comments and indents.]
using UnityEngine;
public class RepeatMovement : MonoBehaviour
{
bool correctLastPosition;
Transform lastTarget;
public Transform target;
Vector3 lastPosition;
CharacterController isHasCharacterController;
void Start()
{
isHasCharacterController = GetComponent<CharacterController>();
}
void Update()
{
// We must temporarily disable the
// CharacterController (if assigned)
// to move our object:
//- - - - - - - - - - - - - - - - - - - - - -//
// Remove this code if there is no CharacterController.
// Only remove the condition if the
// CharacterController is added to the
// player object (this object).
if (isHasCharacterController)
isHasCharacterController.enabled = false;
//- - - - - - - - - - - - - - - - - - - - - -//
// We move our object after the target if it
// exists and we have the correct lastPosition
if (target && correctLastPosition)
{
transform.Translate(target.position - lastPosition, Space.World);
lastPosition = target.position;
}
// Assign lastPosition when the target appears
if (target && !correctLastPosition)
{
lastPosition = target.position;
correctLastPosition = true;
}
// If there is no target,
// it means lastPozition already wrong
if (!target && correctLastPosition)
{
correctLastPosition = false;
}
// When we reassign the target,
// then the lastPosition is also wrong.
if (lastTarget != target)
{
lastTarget = target;
correctLastPosition = false;
}
//- - - - - - - - - - - - - - - - - - - - - -//
// Remove this code if there is no CharacterController.
// Only remove the condition if the
// CharacterController is added to the
// player object (this object).
if (isHasCharacterController)
isHasCharacterController.enabled = true;
//- - - - - - - - - - - - - - - - - - - - - -//
}
}
By changing the target, you can change the binding to another object. And setting the target to null will disable the binding.
_
@JoFalbo
EDITED: I’ve tested your script. It remains a mystery to me. I don’t understand how, but for some reason MovePosition
was ignored by the engine. BUT I found a solution. Enabling Interpolation makes MovePosition
work (and why, I cannot explain). To enable interpolation, select Interpolate or Extrapolate:
My Player movement script is attached to the player.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMoveController2 : MonoBehaviour
{
// PUBLIC
public SimpleTouchController leftController;
public float speedMovements;
// PRIVATE
private Rigidbody _rigidbody;
void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
}
void LateUpdate()
{
_rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
(transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements));
}
}