2011-09-16 3 views
2

Unity3d에서 충돌기가 hit.normal을 사용하여 충돌하는 표면의 법선을 얻을 수는 있지만, Unity3d가 제공되는 것을 어느 쪽이 공격 받았는지 찾는 방법이 있습니까?hit.normal에서 충돌 한 쪽을 찾는 방법은 무엇입니까?

하나의 해결책은 정상의 방향을 보는 것입니다. 정적 인 오브젝트에는 잘 맞아야하지만 방향이 바뀌는 동적 인 오브젝트와 움직이는 오브젝트는 어떨까요?

+0

http://gamedev.stackexchange.com 시도 - 때를 - 게임 개발 –

+0

에 대한 지역 사회 또는 http://answers.unity3d.com 시도 작동 – ina

답변

4
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.”); 
    } 
} 
+2

InverseTransformPoint는 ContactPoint []가 아닌 Vector3를 수신합니다. http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html –

+0

colisión.contactos [0] .point – Warer

0
void OnCollisionEnter(Collision collision) 
{    
    Vector3 dir = (collision.gameObject.transform.position - gameObject.transform.position).normalized; 

    if(Mathf.Abs(dir.z) < 0.05f) 
    { 
     if (dir.x > 0) 
     { 
      print ("RIGHT");  
     } 
     else if (dir.x < 0) 
     { 
      print ("LEFT");    
     } 
    } 
    else 
    { 
     if(dir.z > 0) 
     { 
      print ("FRONT"); 
     } 
     else if(dir.z < 0) 
     { 
      print ("BACK"); 
     } 
    } 
} 
0

이 작동 :

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

    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."); 
    } 
} 
관련 문제