It is something that I have been doing for quite a while.
I have moving platforms? Ok, the player will be a child of each one to make the movements sync.
It is shown in most youtube channels and other tutorials as “the way to do it”. And I admit that i went along.
But today I tried making it differently. Oh, boy. It did not go well.
I tried using velocity of rigidbody, store the last frame position and calculate the velocity and feed it to a character controller, using root motion and passing the deltaPosition, using Parent Constraint.
Neither worked as smooth as setParent.
FFS, it is a rather basic thing. Is the use of SetParent the best way to do it? It seems so boilerplate.
1 Like
When you are using a rigidbody as a character, it is much trickier to get the character to stick to the platform. However, I have recently completed a project that accomplishes getting the rigidbody to stick to platforms that are changing both position and rotation. It takes lots of work, but I’m going to try to lay out a summary really quick.
The whole system relies on creating completely separate physics scenes (Unity - Manual: Multi-scene physics) for each moving platform, which is used to simulate physics.
At first, this simulation scene contains only a copy of the moving platform, except it does not move. When the rigidbody player in our main scene collides with the moving platform, a copy of the player rigidbody is added to the simulation at the same position/speed relative to the moving platform. Then, disable the controller on the main rigidbody player so that it doesn’t respond to player input. Do not disable the controller on the simulated rigidbody - it should continue to respond to the controller and freely move around on the simulated platform.
The simulated rigidbody character is now walking around on a simulated non-moving platform. Now use MovePosition() and MoveRotation() to move/rotate the main rigidbody character to match the simulated rigidbody character’s position and rotation relative to the simulated platform. This way, when the simulated rigidbody character moves, the main rigidbody character will as well.
At this point, we have a functioning sticky moving platform. As long as the character transformations are done with respect to the platform(s), it doesn’t matter how the main platform is moving or rotating - the main rigidbody character will stick to it perfectly.
When the main rigidbody player is no longer colliding with the moving platform, we delete the simulated rigidbody character from the simulation scene, and add full control back to the main rigidbody character.
The hardest part is getting the physics right for the transition from the main scene to the simulation and back.