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

How do I detect collision between colliders?

Discussion in 'Community Learning & Teaching' started by PrecisionGaming, Aug 25, 2014.

  1. PrecisionGaming

    PrecisionGaming

    Joined:
    Nov 6, 2013
    Posts:
    6
    I've tried to searching for the answer, but I couldn't seem to find anything. I need to know how to program it such that my swing animation causes damage. OnTriggerEnter doesn't seem to do anything, although I'm sure I'm doing it wrong. The "enemy" object has a rigidbody, and its collider is a trigger. Also when I check gravity and it falls through the floor the collision works. But when I run into the enemy with my player, which has a collider, nothing happens. Please help.
     
    Last edited: Aug 26, 2014
  2. Tomer-Barkan

    Tomer-Barkan

    Joined:
    Jul 31, 2012
    Posts:
    150
    Keep in mind that for any collision to take effect, one of the objects needs to have a rigidbody attached to it, not just a collider. Collisions always happen between a rigidbody and a collider.

    Next thing to consider, OnTriggerEnter is only called if the collider is marked as "trigger". Make sure that it is.
     
    Nonym likes this.
  3. PrecisionGaming

    PrecisionGaming

    Joined:
    Nov 6, 2013
    Posts:
    6
    Thanks for the reply, I have a rigid body and the collider is set as a trigger. When gravity is checked on the rigid body and it falls through the world the collision works, but when i crash into the object with the player nothing happens.
     
  4. Tomer-Barkan

    Tomer-Barkan

    Joined:
    Jul 31, 2012
    Posts:
    150
    Weird. Pause the game, check that the object with the rigidbody actually touches the trigger collider. Check in all dimensions.
     
  5. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    Not sure if you're saying the player has a collider here, or restating that the enemy has a collider. Can you please clarify what game objects have what components?


    You might want to check OnControllerColliderHit.

    If you're using one of the standard asset character controllers, you could try adding an additional collider to the player to check for the collision.

    If the script that checks is on the enemy, then ensure that the method signature for OnTriggerEnter takes a Collider, and not a Collision, like OnCollisionEnter does.
    CSharp
    Code (CSharp):
    1. void OnTriggerEnter(Collider col) {
    2.   // do stuff here
    3. }
    4.  
    5. void OnCollisionEnter(Collision col) {
    6.   // do stuff here
    7. }
    JS
    Code (JavaScript):
    1. function OnTriggerEnter (col : Collider) {
    2.   // do stuff here
    3. }
    4.  
    5. function OnCollisionEnter (col: Collision) {
    6.   // do stuff here
    7. }