Moving Platform without Setting Parent?

Hi, I want to have a moving platform in my game and have the player stay on that platform and grounded while it moves. The popular choice seems to be to parent the player to the platform but I am getting bugs like scaling issues and the player’s body parts attaching to the platform and away from the player.

How do I get the player to stay on the platform while it moves without parenting it? What are the other ways to do this? Thanks in advance. :slight_smile:

Seems like you are attaching the wrong object. You should only attach the most parent-ish player object as a child to the platform. This should work. Never tried it with different scales, but i assume it works similar to positions, as in scales being relative, so i’d expect no weird scaling to happen, unless you afterwards scale the platform. In which case you’d have to counter-scale the attached player object.

Can be pretty easy, but depends on your implementation of the platform movement. If you do not want to attach the player, you have to simulate the same movement the platform does, on top of the movement the player does. So you control the player like usual, but while “attached” to a platform moving in (1, 0, 0) direction, you add that movement each frame too. If your platform speed is handled as a movement vector that should not be a problem. Otherwise you will have to remember the platform position from last frame, and calculate a delta value to its position in the current frame, which would be the distance and direction moved, which you can then apply to the player position as well.
You’ll also need a mechanism to figure out when a player is supposed to be attached to a platform or not. But this should be the same as if you made the player a child to the platform.

It actually does scale the player when I use the SetParent method, since I made the platform starting with a cube and scaling that bigger, and when the SetParent happens, it also sets the scale to the player as well.

I tried it with just the default cube with no scaling at all and it seems to work but it seems very choppy and slow, and the jumping doesn’t work either.

Thank you for the explanation, but will the player be able to move on the platform while the values are being set constantly?

Also I forgot to mention, I am moving the platform by an animation, if that helps.

You dont set (ie overwrite) values, you add to (ie adjust) values.
The player has a movement vector (x,y,z). The platform has a movement vector (a,b,c). While the player is considered “attached to the platform”, his movement vector is (x+a, y+b, z+c). All of the player movement now simply happens relative to the platform movement, as it would in real life. Assume you were in a real life train, which moved (200,0,0). You’d now, whatever you did, also always move (200,0,0). If you stood up and moved with (x,y,z) in that train, you’d now move with (200+x, 0+y, 0+z), adding your movement, whatever that may be, on top of the trains movement.
So yes, jumping and everything is still possible. When jumping, however, you’ll have to reconsider when you do not want to consider the player “attached” to the platform anymore.

Working with velocities and not just with position changes would probably make this easier, since once accelerated to the platform velocity, the player keeps this velocity until acted upon by natural forces, like gravity. Both ways work tho, and this is rather a question of game design and what is the expected behavior.

All of this should be possible by using the parenting approach tho. I’m not quite sure about the scaling problem, but that would be easily fixable by having the player and the platform share the same parent. So it would look something like that:
-PlatformAnchorObject
–Player
–Platform
Now rescaling the platform has no effect on the player anymore. Anyways, all of these approahces are fine and may have slight advantages or disadvantages in a direct comparison.

Thank you so much!
The parenting solution is great, but for some reason the jumping and movement is slowed down a lot while the player moves with the platform. I’ll try adding the vector next, maybe that will do it. :slight_smile:

EDIT:
I made it so that only when the player jumps he gets de-parented and it’s fine except the choppy movement. Could it be because of the animation? Should I move this platform parent by code? It’s hard to notice in a 30fps recording, but at 60fps it is noticeable.

You would want to move the PlatformAnchor (parent object), yes. As that is what the player would be attached to. So in order to apply the platform movement to the player, which is temporarily a child of the PlatformAnchor, you’ll need to move the PlatformAnchor instead of the Platform itself. The separation of objects only served the purpose of fixing the scaling problems you are having when scaling the platform. So you’d move PlatformAnchor, but scale Platform.

Thanks yeah, that’s what I did. How do I fix the slow movement when the player is a child though? I am getting very slow movement when I am on the platform. I move my character with rigidbody add force, and it is really slow when I get on the platform. I tried both animations and update method as well, same issue. :frowning:

EDIT:
Temporary fix I came up with just now is to disconnect the parent and child when the keyboard movement or jump controls are activated, but the issue is still there. When I move over to the platform, the remaining velocity movement of the player becomes choppy and slow when he gets parented to the platform.

Try this code and attach it to the player. It should work (Use OnTrigger if you don’t have rigidbodies)