使用多个对撞机查找在 GameObject 上发生碰撞的两个对撞机 [英] Find both colliders involved in a collision on GameObject with Multiple colliders

查看:35
本文介绍了使用多个对撞机查找在 GameObject 上发生碰撞的两个对撞机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结如下:我想从 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

当您的脚本从 MonoBehaviour 继承时,会声明一个 gameObject 变量.这个变量指的是这个脚本附加到的游戏对象.您可以使用 gameObject 变量获取一个 GameObject,另一个来自 OnTriggerEnter 函数中的 Collider2D 参数.

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 时首先报告的碰撞数据,然后有另一个 boolean 名为 detectedBefore 的变量,用于确定之前是否已调用过 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 版本是否为false.如果它不是 true,那么这是第一次调用 OnTriggerEnter2D.使用GetComponent 获取其他脚本,然后将other 脚本的boolean 变量设置为true.同时,还使用 ​​OnTriggerEnter2D 中的 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 被调用并且 boolean 变量的 local/this 版本是 true,将其设置为 false 以重置它然后从 OnTriggerEnter2D 函数中获取带有 theOtherCollider 变量和 Collider2D 变量的两个碰撞器.

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.

注意:

YOURSCRIPTOnTriggerEnter2D 函数所在的脚本的名称,它附加到对撞机.您必须将其更改为该脚本的名称.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆