该怎么做呢?
With Collider2D
,那就是Physics2D.IsTouching(collider1, collider2)
但这是 Collider/3D,不同的是,不存在内置 API 来执行此操作。这很复杂但也是可能的。以下是简化的步骤:
1.Use List
of KeyValuePair
存放接触的物体。
static private List<KeyValuePair<GameObject, GameObject>> collisionList;
使其成为static
变量,以便该列表只有一个实例。
2.检测何时有触发器OnTriggerEnter
.
触发检测脚本OnTriggerEnter
函数必须附加到您想要检测彼此接触时的每个游戏对象。
3.检索刚刚互相接触的两个游戏对象。
您可以使用以下方法检索第一个this.gameObject
和第二个 GameObject 通过使用other.gameObject;
. The other
变量来自于Collider other
论据中的OnTriggerEnter
功能。
4现在,检查两个 GameObject 是否存在于collisionList
变量来自#1。如果没有,请添加它们。如果它们已经存在,请忽略它。
5。就像#2,检测何时有触发器退出OnTriggerExit
。这意味着对象不再接触。
6.检索不再像您在中那样相互接触的两个游戏对象#3但在OnTriggerExit
这次的功能。
7现在,检查两个 GameObject 是否存在于collisionList
变量来自#1。如果他们确实将其从该列表中删除。如果他们不忽视的话。
什么对象应该有脚本?
附上CollisionDetection
为您想要用另一个对象检测的每个游戏对象编写脚本。还要确保 Collider 的 IsTrigger 是enabled
以及一个Rigidbody
组件也附加到每个游戏对象。
using System.Collections.Generic;
using UnityEngine;
public class CollisionDetection: MonoBehaviour
{
static private List<KeyValuePair<GameObject, GameObject>> collisionList =
new List<KeyValuePair<GameObject, GameObject>>();
void OnTriggerEnter(Collider other)
{
//Debug.Log("Trigger Entered");
//Get the two Objects involved in the collision
GameObject col1 = this.gameObject;
GameObject col2 = other.gameObject;
//Add to the collison List
RegisterTouchedItems(collisionList, col1, col2);
}
void OnTriggerExit(Collider other)
{
//Debug.Log("Trigger Exit");
//Get the two Objects involved in the collision
GameObject col1 = this.gameObject;
GameObject col2 = other.gameObject;
//Remove from the collison List
UnRegisterTouchedItems(collisionList, col1, col2);
}
public static bool IsTouching(GameObject obj1, GameObject obj2)
{
int matchIndex = 0;
return _itemExist(collisionList, obj1, obj2, ref matchIndex);
}
private void UnRegisterTouchedItems(List<KeyValuePair<GameObject, GameObject>> existingObj, GameObject col1, GameObject col2)
{
int matchIndex = 0;
//Remove if it exist
if (_itemExist(existingObj, col1, col2, ref matchIndex))
{
existingObj.RemoveAt(matchIndex);
}
}
private void RegisterTouchedItems(List<KeyValuePair<GameObject, GameObject>> existingObj, GameObject col1, GameObject col2)
{
int matchIndex = 0;
//Add if it doesn't exist
if (!_itemExist(existingObj, col1, col2, ref matchIndex))
{
KeyValuePair<GameObject, GameObject> item = new KeyValuePair<GameObject, GameObject>(col1, col2);
existingObj.Add(item);
}
}
private static bool _itemExist(List<KeyValuePair<GameObject, GameObject>> existingObj, GameObject col1,
GameObject col2, ref int matchIndex)
{
bool existInList = false;
for (int i = 0; i < existingObj.Count; i++)
{
//Check if key and value exist and vice versa
if ((existingObj[i].Key == col1 && existingObj[i].Value == col2) ||
(existingObj[i].Key == col2 && existingObj[i].Value == col1))
{
existInList = true;
matchIndex = i;
break;
}
}
return existInList;
}
}
USAGE:
只需使用CollisionDetection.IsTouching(object1, object2)
检查两个对象是否接触。
public GameObject foot1;
public GameObject foot2;
void Update()
{
//Check if feet GameObjects are touching
bool touching = CollisionDetection.IsTouching(foot1, foot2);
if (touching)
{
Debug.Log("<color=green>leg1 and leg2 touching</color>");
}
else
{
Debug.Log("leg1 and leg2 NOT touching");
}
}