简体   繁体   English

在Unity中检测碰撞器之间的碰撞

[英]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#. 如何使用C#中的Collision2D在OnCollisionEnter,Exit和Stay方法中的Unity中识别两个不同的BoxCollider2D对象之间的冲突。 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. 然后, 每个孩子都有一个包含OnCollision方法的脚本

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. 您还可以命名每个游戏对象(头部和身体)或更改其标签,然后可以使用this.gameObject.namethis.gameObject.tag标识对撞机,因为它们将在不同的游戏对象中运行。

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. 我认为这简化了游戏逻辑,而不是在同一个游戏对象碰撞函数中处理多个动作,到目前为止,对我来说一直很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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