Wednesday, September 17, 2014

Collision Detection

There are two requirements before detect the collision:

  • Both of the items should have Collider Component;
  • The relative moving object need to add rigid body.
Step 1: Add the Box Collider on the Rock


Step 2: Add the Capsule Collider and Rigid Body on the projectile

Step 3: Write code to control the effect of collision and drag it on Rock
Note: OnTriggerEnter() is a function that can detect the collision. It also can get which object conflicts the object. So, the collider must check the Trigger attribute and set its unique tag.
Step 4: Run the game


  


Another way to detect collision: Do not check Trigger attributes and use OnCollisionEnter() function.

function OnCollisionEnter(collision:Collision){
    if(collision.gameObject.tag=="plane"){
        Destroy(gameObject);
    }
}

The difference between these two methods:
the collider with trigger can enter into the other object while it only conflict at the edge without trigger.

No comments:

Post a Comment