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

2D sprites dont really collide

Discussion in '2D' started by Zerzuskan, Sep 8, 2016.

  1. Zerzuskan

    Zerzuskan

    Joined:
    Sep 8, 2016
    Posts:
    3
    So i have a player that fires bullets (prefabs) and i have an enemy, that should get damage. The Problem is, that it doesnt detect the collision in any way i try it. Tried circle collider2D in the bullets and in the enemy, and i tried to give them rigidbody 2D. Also tried to make colliders a trigger (both) and tried to make rigidbody kinematic. ithink i also tried every combination (like making just the bullet a trigger and so on), but my code keeps useless.

    Code in my bullet prefab :

    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.   {
    3.   if (other.gameObject.tag == "Enemy")
    4.   {
    5.   print("Hit");
    6.   other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGive);
    7.   }
    8.   }
    I also tried replacing "Collider" with "Collision" and use OnCollisionEnter instead of OnTriggerEnter when i dont mark the collisions as trigger. OnTriggerEnter2D seems to be completly not working and i cant compile anymore.

    Code i have in my Enemy prefab:


    Code (CSharp):
    1. public void HurtEnemy(int damage)
    2.     {
    3.         currentHealth -= damage;
    4.     }
    Whatever i tried i didnt even get the "Hit" message. Just in case i tried printing it in Update, and that worked.
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Add "2D" on the end of your ontriggerenter. the functions are different from the 3d physics ones. not surprised you get no hits because there are no 3d colliders colliding with it.
     
    Zerzuskan likes this.
  3. Zerzuskan

    Zerzuskan

    Joined:
    Sep 8, 2016
    Posts:
    3
    Oh your totally right, i changed it to 2D and used Collider2D instead of Collider as Paramtere, that fixed it. Thanks !