- Home /
My child move with the parent but can't move on it when the parent is moving
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.
Hi, I would change the local position of the player. I haven't tested it yet, but if I'm correct it should work:
When the parent of the player is the platform and the platform is moving, the global-position of the player is changing, but not the local-position! And I think to let the rididbody move in local space, you have to do this
So try this:
Vector3 moveDirection = transform.localPosition + (transform.forward leftController.GetTouchPosition.y Time.deltaTime speedMovements) +(transform.right leftController.GetTouchPosition.x Time.deltaTime speedMovements) ;
moveDirection = transform.TransformDirection(moveDirection);
_rigidbody.MovePosition(moveDirection);
Answer by Crown3d · Jun 26, 2021 at 11:07 AM
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?
I want my player to follow the platform, I think parenting was the best choice to do that.
Answer by DenisIsDenis · Jun 28, 2021 at 11:23 AM
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.
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:
I tried to implement this script on my player with the platform as the target. The player follows the platform like it was a child of the platform, but still can't move when the platform is moving
This means that something is wrong with the player movement script. If you can show the script, then I can try to figure out what the problem is.
(Or is there nothing else besides that? If so, where is this function called?)
_rigidbody.MovePosition
(
transform.position
+
transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements
+
transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements
);
I don't know why, but that solution work for me too.
Thank you it helps me a lot !
Answer by JoFalbo · Jun 29, 2021 at 12:03 PM
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));
}
}
I have testet it and it worked (But I used a character Controller and not rigidbody). I have only created a simply scene with 3 cubes (player, target, collider) and added the RepeatMovement script to the player and it worked.
But I had to change the Update-function of the Repeat-script to LadeUpdate() and the player-movement script to Update()
Hope it helped you