In any game with some kind of interaction between in-game objects, colliders are attached to virtually everything. What the players' eyes see are only the textures, meshes and UI elements and their properties like shadows and animations. What a player does not see are colliders – may that be a 2D or a 3D scene, colliders are almost everywhere in a scene.
Ensure the Colliders are Attached
Each of these colliders is attached to some kind of game object to make collisions happen. Colliders are how we find when a game object runs into another, and that's how most of the physics of collision is simulated.
When it seems a collider is not working in your Unity scene, there are a few things that you might want to look at. Check if the colliders corresponding to the game objects are attached to their respective game objects.
If one or both colliders are not attached, that's the first thing you should fix.
Check for Missing Rigid Body
One game object should contain the rigid body component for collision detection between two game objects.
So, check if any of your bodies are missing the rigid body component.
Related article: Mistakes to Avoid as a Unity Beginner
Attach the Right Type of Colliders
There are a lot of different colliders available out of the box in Unity. You might have mistakenly attached the wrong collider type if you're simultaneously working on several projects.
In a 2D scene, you must attach 2D shape colliders with names like CircleCollider or BoxCollider2D. Similarly, in a 3D game, you must attach 3D colliders such as BoxCollider, SphereCollider, MeshCollider or a CapsuleCollider to your object.
Implement Correct Interfaces
Attaching the right types of colliders is only half the job. To detect collisions, you must also implement the correct methods depending on your game type.
If you are creating a 2D game, implement methods that append 2D, such as OnCollisionEnter2D, OnCollisionStay2D, OnCollisionExit2D, etc., to work with collisions.
If you're making a 3D game, implement methods without the trailing 2D in the method names, such as OnCollisionEnter, OnCollisionStay, OnCollisionExit, etc.
Perform similar checks for methods that check for Triggers instead of Collisions in your scene.
Undetectable Collision
In some cases, a collision may not be detectable due to the collision matrix.
The layer-based collision detection technique is a way to make some objects collide with another game object that is present in another layer. Check collision matrix settings in the Physics Settings window (Edit
> Project Settings
> Physics
), and ensure correct settings for collision between the respective layers.
Also read: 10 Rules for Crafting Seamless Test Cases
Using Tags?
If you are trying to check collision using a tag, make sure that you are using the correct tag name in the script.
For example, if you have to check the collision between the bullets and the enemy in any combat game, you might have set the tag enemy
for enemy objects in Unity Editor.
Also, make sure your script references the tag correctly. collider.gameobject.CompareTag("emeny")
looks okay at first, but there's a typo in the tag name. Look closely to see if the tag names match.
Is the Collider a Trigger?
A collision can not be detected if the Is Trigger
attribute is set to the collider. Open the properties pane of the game object and look inside the Collider component to ensure that option is not selected.
If a collider has the Is Trigger
attribute set, instead of the OnCollision...()
callbacks, the OnTrigger...()
callbacks are fired. In such cases, collision detection might seem to be not working.
Something Else...
There might be several other reasons why collision detection might not work. You can try adding a new rigid body and a new collider to another game object and see if it works before looking any further. In most cases, it's a human error, so I hope the above tips will be helpful.
I hope you liked what you read. Consider subscribing below to receive the latest Unity blog tips and tricks!