- Home /
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.$$anonymous$$ove method implementation ?
From what you wrote, assu$$anonymous$$g colliders / rigidbodies are correctly defined, your code could/should work (i don't really get why you're setting $$anonymous$$oveDir.y -= Gravity * Time.deltaTime all the time but it shouldnt matter much if your character jumps on a platform with a collider/rigidbody)
So then it depends how the $$anonymous$$ove method is defined in your CharacterController class (it is not a Unity API class/method)
It would help to see the definition of the $$anonymous$$ove method your using
Answer by nreina · May 22, 2019 at 08:06 AM
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: https://answers.unity.com/questions/1632526/character-controller-doesnt-move-with-a-moving-par.html
Thank you! This worked for me. Though I have no idea why keeping the code in Update() won't work.
Answer by golaod · Jun 09, 2021 at 07:59 PM
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!
Man you're a life saver, 1) I move the platforms with dotween, so I Changed all my tweens to fixedUpdate without luck. 2) Stopping the player movement (don't calling the CharacterController.move(direction) ) makes the moving platform to work.
So I realize that, if I simply avoid the Move function if the user doesn't input any key, the platform should work as expected.
After some time, I realize that this will make the player unable to jump, and the gravity won't work on it, because I'm not moving at all.
Blah blah blah, more tests , blah blah blah.... Maybe changing all my code so the player is controled by a RigidBody instead of the Character controler? I had previous projects with movind platforms on rigidBodies and it should work! mmm but this way, you loose the slope control witch is a PAIN to do it on a rigid body... Blah blah blah, more tests , blah blah blah....
AAAND all that was a waste of time, because what I had wrong was a Unity 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
Answer by unity_I8FaTX-tZFc4lA · Feb 18 at 10:09 AM
The reason why it is not working it's because you are not saving the transform to the physics engine.
https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html
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.