Unity Colliders are one of the most important parameters or components of any gameobject. Colliders are used in almost all the games. Without the basic understanding of how unity Colliders work, it will be very difficult to code the game mechanics.
If you are totally new to Unity check out how to learn Unity.
Unity colliders are very simple to use. First thing that you have to pay attention to is unity has devided the colliders into 2D and 3D. So you have to very careful which collider are you using. Further, there are types, static collider, rigidbody collider, kinematic rigidbody collider.
Static colliders are considered to be non moving objects by unity. Do not confuse static gameobject with static collider. If rigidbody is not attached to a collider then its a static collider. They do not move when an object Collides to with it. Though, they can be moved using transform, moving a static collider leads to performance loss during runtime.
A rigidbody collider works like a real world object. It is governed by the forces of physics. If you apply force on it, it will move. In Unity rigidbody is used on an object which will move a lot during gameplay. Check the screenshot for how a rigidbody is attached to a game object.
Kinematic rigidbody collider
A kinematic rigidbody is a rigidbody that behaves like a static object. So, the next question is then why use a kinematic rigidbody. The main reason to use a kinematic rigidbody is to tell unity that the object does not respond to forces but will be movable by script during runtime. The performance loss is very less in moving an kinematic rigidbody compared to a Static object.
Unity colliders not working! Here is why.
Unity will detect a collision only if one of the object is a rigidbody. That is, if you have two gameobject marked as static collider then collision between them will not be detected. Its the same case with two kinematic rigidbody. If you use OnCollisionEnter function in any of the above case, the function will not be called during a Collision. Find the collision matrix for unity colliders in the image below.
Trigger Unity colliders
When you mark an collider as trigger, it no longer behaves like a solid object. It allows the other object to pass through it. But the time of one object entering other object space can trigger a function. With that you can know if an collision has happened without actually creating a collision. OnTriggerEnter function is used if the collider is Marked as a trigger. The collision matrix is a little different in case of triggers.
Trigger collision doesn’t work if both the colliders are static colliders. In all the other cases the trigger function is called.
Knowing this basic things about colliders will help you set them up easily in your game.