简体   繁体   中英

Detecting Collisions between Colliders in Unity

How can I identify a collision between two different BoxCollider2D objects in Unity in the OnCollisionEnter,Exit, and Stay Methods using Collision2D in C#. Thanks

When I have multiple colliders that should trigger different actions I place them in different game object children .

For example, if I have one character (game object) with two different colliders, one for the head and another one for the rest of the body, that trigger different actions I create two game object inside the character. Then each child will have its script containing OnCollision methods.

You can also name each game object (head and body) or change their tag and then you can identify the colliders using this.gameObject.name or this.gameObject.tag since this will be running in different game objects.

I think this makes game logic simple instead of handling multiple actions inside the same game object collision functions and it has been working good for me so far.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM