Search Unity

C# Collisions not Being Detected!

Discussion in 'Scripting' started by Sporech, Sep 21, 2015.

  1. Sporech

    Sporech

    Joined:
    Sep 14, 2015
    Posts:
    26
    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:
    Code (CSharp):
    1. void OnCollisionEnter (Collision other) {
    2.         print ("Collision");
    3.         if (other.transform.tag == "Enemy") {
    4.             print ("I Hit Enemy");
    5.         }
    6.     }
     
    JonathanSchwenk likes this.
  2. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    So many things can be wrong :)
    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
     
  3. Sporech

    Sporech

    Joined:
    Sep 14, 2015
    Posts:
    26
    Both objects have rigidbodies, the collider is in the same location as the object, and they aren't in kinematic :confused:. I am also not using layers at my stage. (so i guess everything is on the same level, right?).
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    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.
     
    akshaj2007 and moazsabsob like this.
  5. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    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).
     
  6. Sporech

    Sporech

    Joined:
    Sep 14, 2015
    Posts:
    26
    It won't let me upload the scene, how do i do it?
     
  7. alexikari

    alexikari

    Joined:
    Aug 31, 2015
    Posts:
    21
    2D or 3D?
     
  8. ryandavis26

    ryandavis26

    Joined:
    Sep 7, 2020
    Posts:
    1
    do
    Code (CSharp):
    1. void OnTriggerEnter (Collider other) {}
    and it should work
     
  9. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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.
     
  10. mgrekt

    mgrekt

    Joined:
    Jun 22, 2019
    Posts:
    92
    You did add the "Enemy" tag in the inspector?
     
  11. juicy_jona

    juicy_jona

    Joined:
    Mar 18, 2022
    Posts:
    1
    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".

    :)
     
  12. ChadDevGamer

    ChadDevGamer

    Joined:
    Dec 1, 2022
    Posts:
    1
    In the rigid body change collision from discrete to continuous :))