- Home /
Rigidbody on a moving platform
Hi.
I am currently redeveloping our character-controller based player into a rigidbody-based player for Pirates of New Horizons because we got some strange behaviors when switching to unity 3.x from 2.6.
We figured we would have more control if we did things ourselves.
However, I am struggling with having our player be moved by other rigidbodies. More specifically; moving platforms that are underneath the player. The player will be pushed sideways if something hits it from the side, or moved upwards if something underneath pushes upwards, but I cant seem to figure out how a platform moving in the x-z plane can move the player, which is a rigidbody, standing ontop of it.
On the old character controller I had implemented the solution that can be found here: http://answers.unity3d.com/questions/8207/charactercontroller-falls-through-or-slips-off-mov.html but since all the rigidbody movement is applied during FixedUpdate, this isn't working so well.
Does anyone know how I can approach this problem without parenting to the stuff below, because that feels very jerky when I have tried it.
Answer by Lanthuas · Dec 26, 2017 at 03:02 AM
Just add script with this code to platform. Works like charm for me.
void OnCollisionEnter2D(Collision2D other) {
if (other.transform.tag == "Player") {
other.transform.parent = transform;
}
}
private void OnCollisionExit2D(Collision2D other) {
if (other.transform.tag == "Player") {
other.transform.parent = null;
}
}
hey, when i set my player as a child of the platform, it does follow the platform, but i cant move the player if the platform is moving. its as though the controls were disabled... (im using rigidbody.moveposition for player movement) any idea why that might be? thanks in advance!
fixed it. i called moveposition () from update () ins$$anonymous$$d of fixedupdate ()
Thank you, the player is moving with the platform and most importantly the player is still able to move. $$anonymous$$uch Appreciated.
Thanks. I used your approach now on an elevator, but replacing Enter and Exit with Stay. Work very well. I also had the issue with both enemies and player not "following" smoothly the elevator.
Answer by hannesdvl · Mar 09, 2013 at 06:03 PM
Moving the platform using only Rigidbody.MovePosition and Rigidbody.MoveRotation in FixedUpdate fixes slipping/bouncing and gives a very smooth and accurate simulation.
Check the full answer in this question: http://answers.unity3d.com/questions/167404/rigidbody-on-a-platform.html
Answer by thebyus · Jan 06, 2013 at 07:19 AM
I know this is an old thread, but I will answer here anyway, as I have been looking to do essentially the same thing for a while and finally figured it out.
I've been messing around with this for the better part of 3 months. I have a rigidbody WIHTOUT a character controller. I have a character that I need to be able to roll onto and off of moving platforms, so parenting wasn't really an option, and the character controller didn't really allow me to do some of the other things I have going on in the game. The answer I found was to create PhysicsMaterial like was mentioned above. I pretty much maxed out everything to do with friction (either 1, Maximum, or for open ended options, 100,000) and set bounciness to 0 or Min. Make sure to set the FrictionDirection2 in the X and Z axes again, (100,000). Then I added this PhysicsMaterial to the box collider on the platform, and finally, I added a rigidbody to the platform, set the mass at between 100 and 1000 (depending on how it plays) set the Rigidbody to "Is Kinimatic", turned off gravity zeroed everything else out and then played with the velocity of the platform to keep the Character from tipping over. After 3 months, the real key here was adding the Rigidbody to the platform, without it nothing happens (and I have the mass of my Character set to 100,000 on it's rigidbody, so a little platform mass goes a long way!) Good luck!
Anyone doing a platformer should know not to use a CharacterController. I've been working on a 2d platformer and my controls are pretty solid on static platforms. But I get random jitters on moving platforms. The positions appear to be correct and the jitters appear more when the platform moves up or down. For my player movement: playerRB.velocity = m_velocity + platform.GetComponent().velocity I'm basically not using any of the physics provided by a rigidbody but I needed more freedom over my character that a standard CharacterController couldn't offer. Any ideas on how I can fix my jitter?
Answer by Waz · Jun 26, 2011 at 01:33 PM
Have you tried tweaking the PhysicMaterials you are using, and increasing the contact points (eg box not capsule), such that friction does the work for you? That's the real-world answer to your question.... not that game physics is perfect of course! Also be careful of the drag setting on the player RB. Also be careful how you move the platforms. Anyone will slip over if the floor beneath them accelerates from 0 to 1m/s in 0.01 seconds.
I played around a little bit with the physic materials, but haven't really delved into that just yet. As with all game characters I'm not really after 100% real physics for my main character, I just want him to be able to be moved by floating platforms :)
This works perfectly. I added a box collider and turn it on when I am on a platform.
Answer by Azaldur · Sep 26, 2011 at 06:56 PM
Hey Skjalg, did you already found out how to make a player move fine on a moving object? I'm also struggling with this.
After about a week of trying different things I figured that creating a player controller by using a rigidbody is impossible to get to feel good unless you set the velocity each frame. And when you start setting the velocity every frame you are basically rendering the whole rigidbody physics stuff obsolete and you might as well use the Character Controller ins$$anonymous$$d. So I switched to that.
Your answer
Follow this Question
Related Questions
Stay on Moving Platform without Character Controller 1 Answer
Keeping the player on a moving platform? 0 Answers
Make a Platform push a character controller? 2 Answers
CharacterController falls through or slips off moving platforms 2.0 2 Answers
What is the easiest way to push a character controller with a moving platform? 0 Answers