Hi everyone,
I currently create a 3D game with a humanoid character. This character contain a rigidbody and a capsule collider. All works well, but when I collide with a bloc ( which contain a box collider ) which is placed just a little bit under the size of my character, the character will have a velocity insane. Here is some image to illustrate my problem:
At this point, my character is supposed to collide next frame with the red block
But instead of just collide, it earn a crazy velocity on all axis. Here is the next frame:
And then, the character is going to the moon, then fall due to gravity.
Is this a bug from unity, or am i doing something wrong?
Thank you
Well you have to understand how collisions and more important how collision response work. Collisions are detected due to overlap after a physics move. If overlap is detected the physics solver tries to solve that collision by applying penalty forces which are based on the collision normal / hit point and your current velocity. So what’s happening here is that you actually overlap with the top part of your capsule with the ceiling. The collision normal will point downwards at an angle due to the round shape of the capsule. However moving your character along that direction will push it into the ground which will result in another penalty force upwards. This may again push you into the box above, etc.
This might repeat until the collisions / overlaps are all solved or the solver runs out of iterations in which case the problem may continue the next frame. At narrow angles the capsule could bounce quite often between the floor and the ceiling. Not really related but the animation of this 3b1b video might help to understand the issue better. Ideally if conservation of energy can be guaranteed the resulting movement should never be larger than the movement before the collision. However we actually don’t work with accurate physics models in the first place and we have limited number precision so it’s possible that you build up more and more momentum due to the bouncing.
This is essentially a corner case and i wouldn’t call it a bug. You just pushed beyond the borders of reliable approximation of a physics simulation. Also keep in mind that if you consider it a bug, it’s not a Unity bug since Unity just uses PhysX.
edit
I forgot to mention some solutions. Well first of all you should avoid level designs where you could squeeze a slightly oversized capsule into a narrow place. The other solution is to not using a capsule but a simple box collider. Such cases are one of the reasons why most games still prefer using box colliders over anything else (the main reason is speed and simplicity). Actually i would go crazy if games like Quake used capsules. This would require you to jump way earlier at an edge than with a box collider. Capsules are great for getting slightly more realistical behaviour automatically (since you slide off an edge as soon as your capsule center is over the edge). However if you use capsules you have to be more careful during the level design. If there’s an area you should not be able to enter, block the way completely with a collider. This is even done in most FPS games to prevent getting stuck at some decorative pillar at a wall. Designing the visual aspects of a level and designing the physical aspects are often two seperate topics. (stairs often are not actual stairs but have a continuous slope for better movement).
Hi @Bencarbon
I guess, since there is box collider (which should be underneath the player) and the capsule collider of the player collides with each other and creates that insane velocity i guess.
(or)
If the box collider underneath the size of the player collides with the ground plane which has a collider (i.e., box collider below the ground plane or box collider and the ground plane collider are intersecting each other) it might create the insane velocity.
If you are using the box collider just for checking if the player is in ground or in air, try changing from box collider to box trigger collider.