A Complete Guide to Fixing Collision Detection in Unity


Júlio Rodrigues ·

Collision detection in Unity is one of the most confusing aspects of Unity scripting for the aspiring game developer. No matter how many times you try to understand it, there's always a case where your methods aren't called and you have no idea why.

Fear not, choose the options that come closest to your situation and see the probable reason for your OnCollision... or OnTrigger... methods to not be called and fix it.

These options should cover both 2D and 3D, just remember that the 2D version has a 2D appended at the end of both the method name and its parameter (e.g. OnTriggerEnter(Collider) becomes OnTriggerEnter2D(Collider2D)).


Additional Tips

You should not move static objects since this will make the physics engine recalculate the whole world. If something should move but shouldn't be affected by physics forces add a Rigidbody to it and make it kinematic.

Avoid using mesh colliders even if it has the same shape as of a primitive collider, the method of geometric intersection is completely different and the primitive colliders are much way faster.

If you are controlling an object by its transform, you must attach a Rigidbody in order to listen for the collision or trigger events, or in exact terms, for the methods OnTriggerEnter, OnCollisionEnter, etc to be called.

Kinematic Rigidbodies won't listen for collisions, but they can listen for trigger events, if marked as trigger.

For collision detection, what difference does it make to have a trigger collider?

Trigger colliders are less restrictive. Their rigidbodies can be kinematic or not, it doesn't matter, in either case the trigger message is going to be sent (OnTriggerEnter/OnTriggerStay/OnTriggerExit methods will be called). If there's no Rigidbody attached they won't receive trigger messages.

Regular collisions using non-trigger colliders require that at least one of the involved objects have a non-kinematic Rigidbody attached.

"That's too complicated, how can I actually learn it?"

Right, you have fixed your problem but you're still wondering if you'll need to always reference a guide like this when dealing with collisions. Be cool because the answer is no, once you memorize the general gist of how this works you'll get it.

In general, you should start by adding rigidbodies to every object that you want to be able to listen for collisions. It's not necessary and not recommended to add a Rigidbody to a completely static object, a collider alone is sufficient and enough. If you need an object to listen for a collision but doesn't want it governed by the physics engine simulation, you mark it as kinematic.

"Nope, this guide is a farce, my collisions are still not working..."

I'm really sorry if you still aren't able to fix your collisions, my last attempt to help you is recommending you to read the official documentation on Colliders. It's really good and one of the base sources of this article.

Glossary

Collider
A geometrical representation of the object used by the physics engine. Although it has a geometric look, it's not mesh data, the primitive colliders are optimized checks in the algorithmic and geometric sense using just enough data, they are the sphere, box, and capsule collider.
Static Collider
Game objects that contain colliders but no Rigidbody. They will be used by the physics engine but won't move in response to a collision.
Compound Collider
On several occasions only using a single primitive collider won't be enough to approximate the shape of your visual mesh. Colliders on child objects that have no Rigidbody attached are treated as one big collider. Only the parent object might have a Rigidbody.
Dynamic Collider
Game objects that have colliders but also have a Rigidbody, these will be controlled by the physics engine, you shouldn't try to control these objects transform directly, instead use AddForce and AddTorque (unless they are kinematic rigidbodies).
Rigidbody
This is the most fundamental component of the physics engine. Having this component attached to your game object will make it be part of the physics simulation. After attaching a Rigidbody you shouldn't try to control the object directly using its transform, instead, apply forces and torques to it.
Kinematic Rigidbod Collider
If there's no need for physics simulation but you still would like to know about the collisions of this object, make the object kinematic by checking the Is Kinematic checkbox. It can be moved by modifying its transform but it won't respond to collisions and forces like a regular Rigidbody (non-kinematic).

According to the official documentation kinematic rigidbodies should be used for colliders that may move or be enabled/disabled occasionally but that should otherwise behave like static colliders. This is good recommendation indeed since moving static objects is really CPU intensive and should be avoided.

Let's talk!

Haven't fixed your issue but still think I can help? Reach me on twitter @juliomarcos (I'm available for DMs). We can chat about other game development stuff too!

Detailed Unity Tutorials in your inbox

Subscribe if you want to receive articles and tutorials like this one right in your inbox. 

Main References of the article

Classify in:
  • physics
  • collision detection
  • unity
© Bladecast 2018. All rights reserved.