Collisions

Updated on May 15, 2018 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on May 8, 2018

How do I program collisions with an object that has a certain tag?

 

  • Liked by
Reply
0 on May 8, 2018

Do mean you want only specific objects to be able to collide with each other or do you want specific events or behaviours to happen when two object collide based on the tag of the colliding volume.

  • Liked by
Reply
Cancel
0 on May 8, 2018

If you are using colliders, in the function OnEnterCollision (or OnEnterCollision2D, if you are using 2D colliders), the paramether Collider that is automatically passed as a parameter is the collider of the other object.

So, if you want to only do something if the other object’s tag is “foo”, you do something like this:

void OnEnterCollision2D(Collider2D collision){
    if(collision.tag.Equals("foo")){
        //Do whatever you have to do
    }
}
  • Liked by
Reply
Cancel
0 on May 15, 2018

Thanks

 

  • Liked by
Reply
Cancel