Moving Platform and CharacterController.Move not working. The player falls

I am using CharacterController.Move to move the player, and I make the player child of the moving platform to move with it, but it is not working, the player doesn’t move with the platform.

private void OnTriggerEnter(Collider other) 
    {
        if (other.tag == "Platform") 
        {
            transform.parent = other.transform.parent; 
        }
}
private void OnTriggerExit(Collider other) 
    {
        if (other.tag == "Platform") 
        {
            transform.parent = null; 
        }
    }

This up code works well, the player change and takes the platform as parent. But the player falls, and not moving with the platform.

  if (Control.isGrounded)
        {

            MoveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 
            MoveDir *= Speed; 
            JumpCount = 0; 
        }
        else 
        {
            MoveDir = new Vector3(Input.GetAxis("Horizontal") * Speed, MoveDir.y, Input.GetAxis("Vertical") * Speed);
        }
        if (Input.GetButtonDown("Jump") && JumpCount < TotalJumps) 
        {
            JumpCount++;
            MoveDir.y = JumpSpeed;
        }
        MoveDir.y -= Gravity * Time.deltaTime;
        if (ActivateMove)
        {
            if (MoveDir != Vector3.zero)
            {
                Control.Move(MoveDir * Time.deltaTime);
            }  
        }

This is the player moving script. I put a boolean “ActivateMove” to desactivate the movement and check, if is desactivate the player moves with the platorm and it works. But I can’t desactive the movement when the player is in the moving platform becouse I want to move it in the platform too.

Thank you.

Hi @nreina, what's your CharacterController.Move method implementation ?

3 Answers

3

It’s an old topic, but I just had the same issue. I moved my old project to new unity and new render pipeline system and same script, same player and same platform stopped working.


For me changing from Update to FixedUpdate was not the right solution as other triggers on the platform stopped working properly (they worked, but…during specific frames only?).


After checking all the topics with rigidbody, OnTriggerStay, position correction and so on, I found out that there is ‘auto sync transforms’ option which was not enabled in my new project. When I reenabled it, things went back to normal. (Edit → Project Settings → Physics (or Physics 2d) → Auto Sync Transforms

Thank you so much. I literally wasted a whole day trying to get this to work, when it was just a simple setting in unity...

You have no idea how much I was searching for a solution, I didn't know my problem was in the Physics settings. Thank you so much!

Turning on 'Auto Sync Transforms' seems to work for me also, I have tried it before so I am sure that ticking that option really does work, though I strongly advise to use the option cautiously as stated here.: https://docs.unity3d.com/ScriptReference/Physics-autoSyncTransforms.html

My god! I was banging my head against a wall!!! thank you so much!

I finally made it, Changing the Platform code from Update to FixedUpdate, it works well. I don’t know why, but works. Searching I found the solution here: Character Controller doesn't move with a moving parent - Questions & Answers - Unity Discussions

thanks @Meishin @DCordoba for the help

Thank you! This worked for me. Though I have no idea why keeping the code in Update() won't work.

The reason why it is not working it’s because you are not saving the transform to the physics engine.

If you have Physics.autoSyncTransforms enable, which is not by default, could also be a potential fix.
Otherwise, “When set to false, synchronization only occurs prior to the physics simulation step during the Fixed Update.” which explain why the solution you found works.