Unity 检查数组中的二维对象是否在没有 OnCollisionEnter 的情况下发生碰撞

Unity check if 2D objects in an array collide without OnCollisionEnter

我将 Rigidbody2D 和 2D 碰撞器连接到我的游戏对象。有没有办法检查我的任何两个对象之间是否发生碰撞?我找到了一种方法:方法 OnCollisionEnter(),但我想在特定时间检查特定对象之间的碰撞,而不是所有对象之间的碰撞。有没有办法做到这一点?提前致谢!

您可以创建一个数组,将 links 存储到与此对象发生碰撞的对象,然后只需在 OnCollisionEnter 中添加 links 并在中删除 links OnCollisionExit。所以你将能够检查一个对象的数组是否包含 link 到第二个,如果是 - 这两个对象在这一刻发生碰撞。

Is there a way to check if there is a collision between any two of my objects?

要检查两个 2D 碰撞体是否 touching/colliding 而没有 OnCollisionEnter,请使用 Collider2D.IsTouching

例如:

BoxCollider2D myBoxCollider1 = null;
BoxCollider2D myBoxCollider2 = null;

if (myBoxCollider1.IsTouching(myBoxCollider2))
{

}

对于数组,这有点复杂。您必须循环并将每个与另一个进行比较。您必须检查两个对象是否已经比较过并且不再比较它们以防止多重碰撞检测。

例如,在for循环中,如果你不这样做你会得到:

  • 对象 A 与对象 B 发生碰撞

然后

  • 对象 B 与对象 A 发生碰撞。

你不想要这个。您只想检测并报告两个对象之间的一次碰撞。

Dictionary 本来是存储检测到的对象的解决方案,这样我们就不会再次检测到它们,但是 Dictionary 不能多次持有相同的密钥。

您可以使用 ListKeyValuePair 来执行此操作。只需在另一个循环中执行循环,然后使用 IsTouching 进行比较。在比较每个 BoxCollider2D 之前,请检查它们是否都已存在于 List<KeyValuePair> 中。如果他们这样做,就不要比较,如果他们不这样做,就比较它们然后将它们添加到List<KeyValuePair>。完成比较后或在函数结束时,清除列表。

//Plug in from the Editor
public Collider2D[] myBoxColliders = null;

List<KeyValuePair<Collider2D, Collider2D>> usedCollider = new List<KeyValuePair<Collider2D, Collider2D>>();

void checkArrayCollision()
{
    for (int i = 0; i < myBoxColliders.Length; i++)
    {
        checkCollision(i, ref usedCollider);
    }
    //Reset List for next function call
    usedCollider.Clear();
}

void checkCollision(int currentIndex, ref List<KeyValuePair<Collider2D, Collider2D>> usedCollider)
{
    for (int i = 0; i < myBoxColliders.Length; i++)
    {
        //Make sure that this two Colliders are not the-same
        if (myBoxColliders[currentIndex] != myBoxColliders[i])
        {
            //Now, make sure we have not checked between this 2 Objects
            if (!checkedBefore(usedCollider, myBoxColliders[currentIndex], myBoxColliders[i]))
            {
                if (myBoxColliders[currentIndex].IsTouching(myBoxColliders[i]))
                {
                    //FINALLY, COLLISION IS DETECTED HERE, call ArrayCollisionDetection
                    ArrayCollisionDetection(myBoxColliders[currentIndex], myBoxColliders[i]);
                }
                //Mark it checked now
                usedCollider.Add(new KeyValuePair<Collider2D, Collider2D>(myBoxColliders[currentIndex], myBoxColliders[i]));
            }
        }
    }
}

bool checkedBefore(List<KeyValuePair<Collider2D, Collider2D>> usedCollider, Collider2D col1, Collider2D col2)
{
    bool checkedBefore = false;
    for (int i = 0; i < usedCollider.Count; i++)
    {
        //Check if key and value exist and vice versa
        if ((usedCollider[i].Key == col1 && usedCollider[i].Value == col2) ||
                (usedCollider[i].Key == col2 && usedCollider[i].Value == col1))
        {
            checkedBefore = true;
            break;
        }
    }
    return checkedBefore;
}

void ArrayCollisionDetection(Collider2D col1, Collider2D col2)
{
    Debug.Log(col1.name + " is Touching " + col2.name);
}

用法:

只需调用checkArrayCollision();来检查myBoxColliders数组中的哪些Collider相互碰撞。

void Update()
{
    checkArrayCollision();
}

每次检测到碰撞时,都会调用 ArrayCollisionDetection(Collider2D col1, Collider2D col2) 函数,您将在其 col1col2 参数中收到两个碰撞器。

注:

你提到了 Circle Collider 2DBox Collider 2D 但是这个例子使用了 Collider2D 因为 Collider2D 是它们两者的 class 的基础,因此将与它们一起工作。因此,您可以将 Circle Collider 2DBox Collider 2D 拖到 myBoxColliders 插槽中。