- Home /
[2D] Rigidbody trips when walking
Hi
I'm creating a small 2D playformer game in Unity, and in this game I want to have a few moving enemies. I've managed to get the scripts and everything just right, but now the problem is, that it looks like the zombie (the enemy I'm using) seems to fall over at random points during his walk.
I've tried moving the ground tiles to flatten it more, making the zombie's collider box be horizontally flat at its feet, I even tried making an invisible collider box just above the ground for it to walk on, but it still does the same thing every time.
Here's screenshots on how I have its box collider set up (I took off the trigger collider just for testing purposes):
I'm completely clueless on what might be causing this problem and would very much appreciate advice from someone more experienced than me.
When I had similar issues I learned that using edge colliders for your terrain eli$$anonymous$$ates the issue.
Can I just say that, for the record, that looks really good.
@GregoryNeal That seems to have been the issue, I've had a few test runs and he hasn't tripped over his own feet anymore, thank you!
@Pflobus Thank you very much, it's my first project I'm doing by myself, I appreciate it a lot!:)
Answer by zach-r-d · Jun 20, 2015 at 12:08 AM
This is being caused by several adjacent colliders being used for a single contiguous surface (i.e. one box collider per tile); it's a known limitation of Box2D, the engine that Unity's 2D physics is based on. The "Why does the character get stuck" section of this article might give a better understanding of what's happening. While the article goes on to suggest some solutions for Box2D, the fix in Unity is to use an edge collider that runs along the border between solid and non-solid tiles.
As an aside, if you don't want the zombie to fall over, you can enable the Freeze Rotation option in the Constraints section of the Rigidbody 2D component in the inspector.
Thank you so much for your answer! You've been very clear and thorough, and my zombie hasn't fallen over in the test runs I did, I'm pretty sure this fixed it!
Answer by tyxman · Jun 20, 2015 at 05:27 PM
This problem from what I can see can't be fixed. I had a similar issue, but from my research you can't use BoxColliders 2D, use 4 edge colliders. Also If your player is falling and you don't want it to in the Rigidbody check off FixedAngle. That might fix the other issue as well but he may start to stop randomly instead.
This is the explaination to why it does that. http://www.iforce2d.net/b2dtut/ghost-vertices
Answer by kavanavak · Dec 23, 2017 at 01:18 AM
This can be solved now using the "Composite Collider 2D"
Answer by nguyenlamlll · Jan 31, 2018 at 02:40 PM
As @kavanavak answered, Composite saves us! In case people still come here for the same problem. Here's a tutorial video.
In short, on the object which has tilemap collider 2D component, add Composite collider 2D (also, RigidBody 2D will be automatically added if there is none attached already.) Next, in tilemap collider, check the box "Used by Composite". Now instead of having a collider for each square, we have a big collider for the whole shape.