如何从hit.normal中找到碰撞器的哪一侧被击中?

Spe*_*ine 3 collision-detection unity-game-engine game-physics

在 Unity3d 中,我可以使用 hit.normal 获得对撞机碰撞的表面的法线,但是有没有办法找到哪一侧被击中了 Unity3d 提供的内容?

一种解决方案是查看法线的方向,对于静态对象应该很有效,但是对于方向发生变化的动态和移动对象呢?

jac*_*ith 5

function OnCollisionEnter(collision : Collision)
{
    var relativePosition = transform.InverseTransformPoint(collision.contacts);

    if(relativePosition.x > 0) 
    {
        print(“The object is to the right”);
    } 
    else 
    {
        print(“The object is to the left”);
    }

    if(relativePosition.y > 0) 
    {
        print(“The object is above.”);
    } 
    else 
    {
        print(“The object is below.”);
    }

    if(relativePosition.z > 0) 
    {
        print(“The object is in front.”);
    } 
    else 
    {
        print(“The object is behind.”);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • InverseTransformPoint 接收 Vector3,而不是 ContactPoint[]。http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html (3认同)