1 - 您可以遍历接触点并检查法线。
void OnCollisionEnter(Collision collision)
{
// Loop through all contact points
foreach (ContactPoint contact in collision.contacts)
{
// Check the normal of each contact point
if (contact.normal.y > 0)
{
// The top of the collider was hit
Debug.Log("Top hit");
}
else if (contact.normal.y < 0)
{
// The bottom of the collider was hit
Debug.Log("Bottom hit");
}
else if (contact.normal.x > 0)
{
// The right side of the collider was hit
Debug.Log("Right hit");
}
else if (contact.normal.x < 0)
{
// The left side of the collider was hit
Debug.Log("Left hit");
}
}
}
您还可以使用第零个索引来检查最近的点,而不是遍历所有点。
2 - 您可以使用对撞机的ClosestPoint。
3 - 检查方向
private void OnTriggerEnter (Collider other) {
Vector3 direction = other.transform.position - transform.position;
if (Vector3.Dot (transform.forward, direction) > 0) {
print ("Back");
}
if (Vector3.Dot (transform.forward, direction) < 0) {
print ("Front");
}
if (Vector3.Dot (transform.forward, direction) == 0) {
print ("Side");
}
}