- Home /
Unity physics bug, falling through terrain
I have a terrain collider with my terrain and my player (3d model with own controller, rigid body, box collider...) can walk on terrain but some random times, when it collides with a wall or another gameObject, it starts falling through terrain as if it was "quicksand" then it falls with gravity. I guess it is because the collision may try to push the player down on Y axis and due to some instant lag or something, it goes through terrain then it no longer detects collision.
Is there any way to solve this?
Answer by Haerius · May 28, 2016 at 02:01 PM
I would suggest to use a "Capsule Collider". Also, setting the "Collision Detection" of your Rigidbody to "Continuous Dynamic" should help minimize missing collisions.
If you are not using the controller provided with the Unity Standard Assets, I could imagine, that you maybe are directly accessing the transform.position
to move the character. If so, this would be the cause of your problem. It is important to not directly change the characters Transform component, but instead use the Rigidbody to move the character. Here is the official tutorial about Character movement: https://unity3d.com/learn/tutorials/projects/stealth-tutorial-4x-only/player-movement
This is a basic but VERY useful tip (not accessing the player's transform position directly).
I have not personally tried to use the Capsule Collider however I will give it a try!
I think at some point, everyone made that mistake ;) Also maybe I should add, that it is very important to do all physics related code inside the "FixedUpdate". Since changing the rigidbody is also physics related, it has to be inside the FixedUpdate method. Shameless plug: If anyone is interested, I talked about a similar issue in my blog post #7 http://goingmadgames.com/blog.php
I'm using rigid body velocity not translations by the way, why would you use capsule collider ins$$anonymous$$d?
If we are talking about a humanoid character, a capsule is just a more appropriate primitive shape than a box. And just in general, it is easier to navigate around with an capsule as a collider, since it won't get stuck on small steps/uneven terrain. Regarding your problem, the only thing I can think of would be to make sure, that you put your rigidbody translation code inside the "FixedUpdate" method.