C# Collisions not Being Detected!

Hi, i have a simple player and enemy object. The enemy will be able to destroy the player on contact; but for now im just making it print to the console. As the name suggests, it isnt detecting the collisions, it just ignores it. I’ve followed this code exact from a tutorial, but it still doesn’t work (could be outdated). Both objects have is trigger unchecked. Can anyone see anything wrong with it:

void OnCollisionEnter (Collision other) {
        print ("Collision");
        if (other.transform.tag == "Enemy") {
            print ("I Hit Enemy");
        }
    }
1 Like

So many things can be wrong :slight_smile:
First check that at least one of the objects has a rigidbody on them. Make sure the rigidbody is not set to kinematic and make sure it is on the same transform as the collider.
Second , make sure the layers aren’t ignoring each other by going to Edit / project settings / physics

1 Like

Both objects have rigidbodies, the collider is in the same location as the object, and they aren’t in kinematic :face_with_spiral_eyes:. I am also not using layers at my stage. (so i guess everything is on the same level, right?).

OnCollisionEnter is for 3D physics, so ensure you’re not using Rigidbody2D or any 2D colliders.

Beyond that, this code looks correct, so the problem must be elsewhere. Upload your scene so we can take a look.

2 Likes

There’re some additional tips from my experience :

• If the collision is quick, more often than not, I had to make the collision box a little bit bigger than what I need (visually) for the collision to be recognized. Seems like, sometimes, the OnCollisionEnter isn’t recognized while OnCollisionStay is.
• For any Mesh Collider, make sure they are convex. Otherwise, while they might be recognized for real-time collision (bounce and halt), they won’t be recognized by OnCollisionEnter.

Also, depending on what you wish to do with your collision. If it’s projectile (like a bullet), I would suggest to use a trigger instead of a standard collision for the projectile. OnTriggerEnter is actually a lot more liable than OnCollisionEnter since OnTriggerEnter is called at the first frame from which the collider is in contact (or inside) the trigger collider’s zone while OnCollisionEnter can be missed as the collider bounce (force away) push the colliders away too fast for there to have the collision recognized.

If you wish to have a standard collider and a hit-zone on the (Enemies for instance), you could use 2 colliders : One mesh or basic collider for the bounce/halt and 1 trigger collider that extend slightly further from the basic collider. Then by using OnTriggerEnter, you won’t have any problem (in theory if you set things right).

2 Likes

It won’t let me upload the scene, how do i do it?

2D or 3D?

do void OnTriggerEnter (Collider other) {} and it should work

This only applies if the Collider’s “IsTrigger” field is set.
Plus, this thread was created 5 years ago; pretty sure the OP isn’t having this problem anymore.

3 Likes

You did add the “Enemy” tag in the inspector?

i had the same problem but i found the solution (at least for me).
in the rigid body component of one of the colliding game objects you have to change the setting of “collision detection” from “discrete” to “continuous”.

:slight_smile:

In the rigid body change collision from discrete to continuous :))