Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Collision detection is not working

Discussion in 'Scripting' started by Tornado77, Dec 20, 2018.

Thread Status:
Not open for further replies.
  1. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Hello, i have a little problem, my script wont detect collision with the ennemies, i tried with other gameobject but its not working!
    Code (CSharp):
    1. void OnCollisionEnter(Collision col)
    2.     {
    3.         if (col.gameObject.name == "Monster!(Clone)")
    4.         {
    5.             Debug.Log("has collide");
    6.             currentHealth -= 5;
    7.             healthBar.value = CalculateHealth();
    8.         }
    9.     }
     
  2. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Just some basic troubleshooting: if you place a Debug.Log call above your condition, does that execute? This tells whether the problem lies in the collision or in the subsequent name comparison.
     
  3. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    no its not executing
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Did you see this part of the manual?
    "Note: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions."
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html

    Also, checking with a name like that is no good, super prone to error and very limited.
    My suggestion to you is to use tags.
     
    Isaac919113 likes this.
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Or checking against the script directly can avoid nasty tags as well.
    E.g.
    Code (CSharp):
    1. Monster mon = col.GetComponent<Monster>();
    2. if (mon != null) // Do something
     
  6. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Why do its not working with a rigidbody?
     
  7. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Do i need to activate "IS trigger"?
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
  9. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
  10. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    the script is on my player
     
  11. diasrodrigo

    diasrodrigo

    Joined:
    Jul 4, 2017
    Posts:
    420
    You are using OnCollisionEnter instead of OnTriggerEnter. I think it's the problem, and you are also comparing with game object name, and you should use tags instead. Try something like this:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.tag == "Ennemies") // I have used the same tag that you attached to your enemy character
    4.         {
    5.             Debug.Log("has collide");
    6.             currentHealth -= 5;
    7.             healthBar.value = CalculateHealth();
    8.         }
    9.     }
    Hope it helps you.
    Remember to set one of the colliders as IsTrigger
     
  12. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    diasrodrigo likes this.
  13. Fajky

    Fajky

    Joined:
    Aug 12, 2022
    Posts:
    2
    thx xd
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Please don't necro threads like this. If you appreciate a post, just hit like.

    Thanks.
     
    AdamAgueroPadilla likes this.
Thread Status:
Not open for further replies.