Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Collision detection

Discussion in 'AR' started by Paullux, May 1, 2018.

  1. Paullux

    Paullux

    Joined:
    Aug 6, 2015
    Posts:
    6
    Hello,

    I'm pulling my hair out, I need to detect collisions in ARcore game.

    The player's avatar and enemies/nices have a rigibody and a collider capsule, I must have made a mistake but I haven't seen it for three days. (For information, the enemies/nices are GameObjects with "rigidbody" and "capsule collider" which have in children a nice and an enemy who evolve during the game.)

    I wrote this code to attach to enemies:

    Code (CSharp):
    1.     void OnCollisionStay(Collision other)
    2.     {
    3.         Debug.text = "Name = " + other.gameObject.name + "\n" + "Tag = " + other.gameObject.tag;
    4.         if (other.gameObject.name == "Soldier" || other.gameObject.tag == "ZombieOrHumain")
    5.         {
    6.             if(other.transform.GetChild(0).gameObject.activeSelf && other.gameObject != transform.gameObject)
    7.             {
    8.                 if (!zombiesAttack)
    9.                 {
    10.                     StartCoroutine("ZombieAttack");
    11.                     other.gameObject.GetComponent<PointDeVie>().setHP(other.gameObject.GetComponent<PointDeVie>().getHP() - 4);
    12.                 }
    13.             }
    14.         }
    15.         rb.angularVelocity = Vector3.zero;
    16.         rb.velocity = Vector3.zero;
    17.     }
    18.     void OnCollisionExit(Collision other)
    19.     {
    20.         if (other.gameObject.name == "Soldier" || other.gameObject.tag == "ZombieOrHumain")
    21.         {
    22.             if (other.transform.GetChild(0).gameObject.activeSelf && other.gameObject != transform.gameObject)
    23.             {
    24.                 StopCoroutine("ZombieAttack");
    25.                 zombiesAttack = false;
    26.             }
    27.         }
    28.     }
    colliders and rigidbody capsules have these settings:


    And collisions are not detected and you never enter OnCollisionStay, I don't understand why...

    For the record, I'm working on this game: