使用多个对撞机找到与GameObject发生碰撞的两个对撞机 [英] Find both colliders involved in a collision on GameObject with Multiple colliders
问题描述
总结如下:
我想从OnTriggerEnter2D事件中找到发生碰撞的两个对撞机。我该怎么办?
To sum up the following: I want to find both colliders involved in a collision, from an OnTriggerEnter2D event. How can I do this?
我有两个游戏对象。两者都有对撞机和触发器。
I have two gameobjects. Both have a collider and a trigger.
在对象A上,它被触发器包围。在对象B上,触发器仅围绕特定部分。
On object A, it's surrounded by the trigger. On object B, the trigger only surrounds a certain section.
当对象A的触发器碰到对象B的任何对撞机时,无论是否触发:我希望对象B能够失去健康。反之亦然。
When the trigger of object A touches any collider, trigger or not, of object B: I want object B to lose health. And vice versa.
但是,当对象A的触发器碰到对象B的对撞机(而不是触发器)时,两个对象都会失去健康状态。
However, when the trigger of object A touches the collider (not trigger) of object B, both objects lose health.
我在控制台中得到这个
Object A hit Object B
Object B hit Object A
我得出的结论是,对象A的触发器正在调用对象B的Ontrigger2d事件
I came to the conclusion that the trigger of object A was calling the Ontrigger2d event on object B.
我认为处理此问题的最佳方法是找到哪个对撞机发现了碰撞,并据此:忽略碰撞。
I think the best way to deal with this, is to find which collider 'found' the collision, and depending on that: ignore the collision..
我如何找到哪个触发器找到了碰撞?
How can I find which trigger 'found' the collision?
[也发布在Unity论坛上]
[Also posted on Unity Forums]
编辑:代码
private void OnTriggerEnter2D(Collider2D collision)
{
Consumeable con = collision.GetComponentInParent<Consumable>();
if (con != null && con.gameObject != gameObject)
{
Debug.Log(gameObject.name + " hit " + con.gameObject.name);
con.Damage(1);
}
}
推荐答案
总而言之,我想找到参与碰撞的两个对撞机
To sum it up, I want to find both colliders involved in a collision
有一个<$ c当脚本从 MonoBehaviour
继承时,声明了$ c> gameObject 变量。此变量引用此脚本附加到的GameObject。您可以使用 gameObject
变量获得一个GameObject,并通过 Collider2D
参数获得另一个GameObject。 > OnTriggerEnter 函数。
There is a gameObject
variable declared when your script inherits from MonoBehaviour
. This variable refers to the GameObject this script is attached to. You can get one GameObject with that gameObject
variable and the other one from the Collider2D
argument in the OnTriggerEnter
function.
void OnTriggerEnter2D(Collider2D collision)
{
GameObject obj1 = this.gameObject;
GameObject obj2 = collision.gameObject;
Debug.Log("Triggered Obj1: :" + obj1.name);
Debug.Log("Triggered obj2: :" + obj2.name);
}
编辑:
对象对我没用。我需要对撞机。不,我不能
使用'getcomponent',因为它们有多个对撞机,而我
只需要那些在碰撞中的对撞机
The objects are useless to me. I need the colliders. And no, I can't use 'getcomponent' because they have more than one collider, and I need only the ones in the collision
应该将对撞机作为GameObject的子对象,并且必须将脚本附加到每个子对撞机上,然后此答案中的内容才起作用。
The colliders should be made child of the GameObject and the script must be attached to each child collider then what's in this answer should work.
如果出于某些原因必须在不使该GameObject的碰撞子成为子对象的情况下执行此操作,则可以使用布尔变量仅一次检测碰撞。
If some reason you must do this without making the colliders child of that GameObject then use a boolean variable to detect the collision once only.
这是对此帖子中答案的修改。
This is a modification of an answer from this post.
具有名为 theOtherCollider
的本地 Collider2D
变量以首先存储碰撞数据在调用 OnTriggerEnter2D
然后有另一个名为 detectedBefore $ c $的
boolean
变量时报告c>确定之前是否已经调用过 OnTriggerEnter2D
。
Have a local Collider2D
variable named theOtherCollider
to store the collision data first reported when OnTriggerEnter2D
is called then have a another boolean
variable named detectedBefore
to determine if the OnTriggerEnter2D
has been called before.
调用 OnTriggerEnter2D
时,请检查该 boolean 的 local / this 版本/ code>变量为
函数中的值。 false
。如果不是 true
,则这是第一次调用 OnTriggerEnter2D
。使用 GetComponent
获取其他脚本,然后将 other 脚本的 boolean
变量设置为 true
。同时,还使用 Collider2D
other 脚本上初始化 theOtherCollider
变量>
When OnTriggerEnter2D
is called, check if the local/this version of that boolean
variable is false
. If it's not true
then this is the first time OnTriggerEnter2D
has been called. Use GetComponent
to get the other script then set the boolean
variable of the other script to true
. At the-same time, also initialize the theOtherCollider
variable on the other script with the Collider2D
value from the OnTriggerEnter2D
function.
现在,如果 OnTriggerEnter2D
被调用,并且该布尔值
变量的本地/当前版本是 true
,将其设置为 false
进行重置,然后使用 theOtherCollider
变量和 Collider2D
来自 OnTriggerEnter2D
函数的变量。
Now, if OnTriggerEnter2D
is called and the local/this version of that boolean
variable is true
, set it to false
to reset it then obtain both colliders with theOtherCollider
variable and the Collider2D
variable from the OnTriggerEnter2D
function.
这可能会令人困惑,但是通过查看代码,它更容易理解。
This may be confusing but by looking the code, it is easier to understand.
注意:
YOURSCRIPT
是脚本的名称, OnTriggerEnter2D
函数是里面,并且连接到碰撞机。您必须将其更改为该脚本的名称。
YOURSCRIPT
is the name of the script the OnTriggerEnter2D
function is inside and that is attached to the Colliders. You must change it to whatever that script is named.
public bool detectedBefore = false;
public Collider2D theOtherCollider;
void OnTriggerEnter2D(Collider2D collision)
{
//Get both colliders then exit if we have already ran this code below
if (detectedBefore)
{
//Reset
detectedBefore = false;
//Get both Colliders once below
Collider2D col1 = theOtherCollider;
Collider2D col2 = collision;
Debug.Log("Triggered Obj1: " + col1.name);
Debug.Log("Triggered obj2: " + col2.name);
return; //EXIT the function
}
//Set the other detectedBefore variable to true then set get the first Collider
YOURSCRIPT myScript = collision.gameObject.GetComponent<YOURSCRIPT>();
if (myScript)
{
myScript.detectedBefore = true;
myScript.theOtherCollider = collision;
}
}
这篇关于使用多个对撞机找到与GameObject发生碰撞的两个对撞机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!