我的游戏对象附加了 Rigidbody2D 和 2D 碰撞体。有没有办法检查我的任何两个对象之间是否发生碰撞?我找到了一种方法:方法 OnCollisionEnter(),但我想在特定时间检查特定对象之间的碰撞,而不是所有对象之间的碰撞。有没有办法做到这一点?提前致谢!
最佳答案
Is there a way to check if there is a collision between any two of my objects?
在没有 OnCollisionEnter
的情况下检查两个 2D 碰撞器是否接触/碰撞, 使用 Collider2D.IsTouching
.
例如:
BoxCollider2D myBoxCollider1 = null;
BoxCollider2D myBoxCollider2 = null;
if (myBoxCollider1.IsTouching(myBoxCollider2))
{
}
对于数组,这有点复杂。您必须循环并将每个与另一个进行比较。您必须检查两个对象是否已经比较并且不再比较它们以防止多重碰撞检测。
例如,在for循环中,如果你不这样做你会得到:
- 物体 A 与物体 B 发生碰撞
然后
- 物体 B 与物体 A 发生碰撞。
你不想要这个。您只想检测并报告两个对象之间的一次碰撞。
Dictionary
本来是存储检测到的对象的解决方案,这样我们就不会再次检测到它们,但是 Dictionary
不能多次持有同一个 key 。
您可以使用 List
和 KeyValuePair
去做这个。只需在另一个循环中执行循环并制作和使用 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)
函数将被调用,您将在其 col1
中收到两个碰撞器。和 col2
参数。
注意:
您提到了 Circle Collider 2D 和 Box Collider 2D 但此示例使用了 Collider2D
因为 Collider2D 是它们两者的类的基础,因此将与它们一起工作。因此,您可以将 Circle Collider 2D 和 Box Collider 2D 拖到 myBoxColliders
插槽。
关于c# - Unity 检查数组中的二维对象是否在没有 OnCollisionEnter 的情况下发生碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40723179/