Search code examples
c#collision-detectionunity-game-engine

Can't get collision or trigger to work with 2 2D objects


I have 2 sprites, both with "Box Collider 2D" component attached. I put the same detection functions in scripts attached to both objects:

void OnTriggerEnter2D(Collider2D coll) {
    Debug.Log ("Something triggered with me!!!");
}


void OnCollisionEnter2D( Collision2D coll ) { 
    Debug.Log ("Something collided with me!!!");
}
  • This is Unity 5.2.1f1
  • I've tried both trigger checked and trigger unchecked. I would prefer to use trigger because I don't need the objects to physically affect each other.
  • I've verified the spelling of the function names and parameters. I've also tried removing both parameters.
  • There is NO 2D rigid body on either object (I don't need one, do I?)
  • Movement is currently being handled by a Translate() in the Update() loop of the objects.
  • I've verified using "Edit Collider" that there is a decent box around each.
  • I've RTFM'ed and read through most of the issues everyone else has seen here but I can't see what I am missing. I've placed all collidable objects on the same layer called "Character" and it appears in the 2D collision matrix in the project settings and is checked.

enter image description here

Any suggestions?


Solution

  • If i am not mistaken you should add a rigidbody2d.

    Just try it. And write a short line of code which resets the other collider (the object that is moving towards your player object / testing object).

    I am not at home to test it for you but this is my guess ;)