- Home /
Player rigidbody slides off moving platform
Hello everyone
I've gotten myself stuck on a problem for a while now. My player, which is controlled using my own script and a rigidbody2d, slides off my horizontally moving platform when standing on it. The hope is to have my player move around freely on the platform while it's moving, without sliding.
I've implemented the use of Rigidbody2D.MovePosition() to move the platform as it was suggested here: http://answers.unity3d.com/questions/167404/rigidbody-on-a-platform.html, but the player still slides off. Perhaps I'm missing something to make this work?
I also tried adding the player as a child of the platform when standing on top of it, but that resulted in really clunky movement for the player. I also don't think it's a very logical solution so I'd like to avoid it if possible (unless anyone can convince me otherwise).
Last I've tried my own solution, which was to add the velocity of the platform to the player's, but that didn't seem to work either. I'm not entirely certain why it didn't work or if I have made some coding mistake, but the logic seemed right I thought.
So if anyone could help me figure this out in a logical and clean way, I'd be very grateful!
Ps: If you need me to supply any of my code or a video of what's going on, let me know.
Answer by Eno-Khaon · Sep 03, 2015 at 08:03 PM
Usually, some of the better options do still involve making the platform a parent of the character for this sort of thing. That way, when the platform begins moving, the player moves exactly with it, as though it were simply stationary ground the player is standing on.
What causes problems with this easily, however, is making the transition between a moving platform and a stationary one (and vice versa). When the parent of a physics-driven object changes, the object keeps its current velocity... and adds it to the parent's. As a result, landing on a platform moving the same direction means suddenly shooting forward at incredible speed.
It does take a little tweaking to get the exact result you want, but unless the platform leans into its motion to push the character standing on it or, ideally for gameplay, make the character a child of the platform, you're simply adhering to the laws of physics.
Thanks for your answer, sadly it hasn't fixes the problem, my player is still sliding alot on the platform when it's set as the players parent. The player is moved a little by the platform because of friction but it's nowhere near enough to keep it in place. Do you have any idea if I'm doing something wrong or what else I could do to fix it?
Also, I have currently made a small trigger collider on top of the platform to trigger the parenting and unparenting, but when the player nears the edges of the trigger field, the player gets scaled in a really weird way that I don't understand. I'm assu$$anonymous$$g it has to do with unparenting of the player, and perhaps quick parenting and unparenting messing up the scale by switching actual scale and localscale too fast or something.
Hmm... for the problem of still sliding around on the platform, how are you moving the platform around? A lack of synchronization between the player's movement and the platform's movement is what causes the character to be thrown about easily.
As far as scale changing, that's always been a quirk of a parent object having a non-uniform local scale. For example, a uniform scale is (20, 20, 20) where non-uniform is (5, 1, 5).
If an object has non-uniform scale, a child object of it shares that parent's scale, then has its own applied. The problem is that this can't appropriately account for any changes in orientation, so while you'll look right when neither object is rotated, any change will result in the child see$$anonymous$$g to stretch to fit the parent's scale applied to itself at its new angle.
I use this method to move the platform:
rb.$$anonymous$$ovePosition(new Vector2(rb.position.x + speed * direction * Time.deltaTime, rb.position.y));
I can see the player being placed corretly as a child to the platform while playing the game.
In regards to the scaling problem, is the solution then to make an empty game object to parent the plaform? Because I've had some trouble trying to implement that :/
Hmm... I don't see anything that looks like it should behave in an anomalous manner for the platform's movement, so as an additional bit of research, you might see whether the solutions to this similar dilemma might be resourceful to you.
As far as the scaling, yes, it's probably handy to have a container object to hold the platform, give that the rigidbody, then give the platform the collider. Then, as the parent object, the container would influence the character's movement, but the platform itself is the actual point of contact.
I got the scaling problem fixed! Thanks, I had previously had problems with understanding how to split the components between the "empty" object and the platform, I didn't realise I could have a rigidbody without a collider on it, even though I don't really see why not, now that I think about it.
But.. I tried implementing the solution you linked, which is similar to the solution I tried to implement myself earlier, and after a bit of debugging it seems that the platforms velocity is constantly 0, which might also explain why setting the player as a child of the platform doesn't work either. This I don't understand though as $$anonymous$$ovePosition() is supposed to also do collision detection and do physics collisions and such. I haven't been able to find any answers to the problem while searching, so if you haven't lost all hope in me yet, I'd love if you could help me out here.
I have this same problem were you able to figure it out? :D
Your answer
Follow this Question
Related Questions
Rigidbody2D adding force vs modifying velocity for character jump 1 Answer
2D rigidbody makes my character slowly rise 1 Answer
"Freeze Position" applied to an axis affects the bounciness of the object's collider on that axis. 0 Answers
Objects stop, but then pass through eachother, Rigidbody2D 0 Answers