2016-06-13 42 views
2

兩部動畫車對象既包含剛體箱撞機腳本OnTriggerEnter事件檢查哪一個對象與另一物體碰撞而腳本是相同

現在我想檢查兩輛汽車運行時哪輛車撞到另一輛車。意思是如果因爲兩者具有相同的腳本和相同的事件,所以命中B或B命中A,兩個事件都成爲觸發器。如何識別誰擊中???

切記請不要推薦我使用光線投射它是昂貴的

比如我做了兩個立方體添加盒撞機和剛體和包含腳本(或檢查)

void OnTriggerEnter(Collider c) { 
     Debug.Log("GameObject is : " + gameObject.name + " and collider : " + c.name); 

    } 

現在,當我觸發這兩個對象觸發器的順序保持不變,無論我打A對象到B或B對象到A.我將如何區分它?

+0

難以承擔的事情。我建議發佈你的嘗試,並解釋你在哪裏阻止。 –

+0

添加了一些細節的代碼片段 –

+0

檢查對象的速度(假設擊中的速度更快)或使用更多的對撞機(只能用前一個擊中) –

回答

0

我看到幾個途徑,其中這是最簡單的:

編輯:注意 - RigidBidy需要有速度,我的意思是,你需要使用AddForce或任何其他物理運動爲基礎RigidBody移動對象。

  1. 檢查速度。 獲取RigidBody每輛車和檢查velocity - 一個更高的velocityVector3是一個打。請注意,您需要在這裏使用投影。

    void OnTriggerEnter(Collider c) 
    { 
        Debug.Log("GameObject is : " + gameObject.name + " and collider : " + c.name); 
    
        Vector3 directionB = this.transform.position - c.transform.position; 
        Vector3 directionA = c.transform.position - this.transform.position; 
    
        Vector3 rawVelocityA = this.GetComponent<Rigidbody>().velocity; 
        Vector3 rawVelocityB = c.GetComponent<Rigidbody>().velocity; 
    
        Vector3 realSpeedA = Vector3.Project (rawVelocityA, directionA); 
        Vector3 realSpeedB = Vector3.Project (rawVelocityB, directionB); 
    
        if (realSpeedA.magnitude > realSpeedB.magnitude) 
        { 
         Debug.Log ("A hit B"); 
        } 
        else if (realSpeedB.magnitude > realSpeedA.magnitude) 
        { 
         Debug.Log ("B hit A"); 
        } 
        else 
        { 
         Debug.Log ("A hit B and B hit A"); 
        } 
    } 
    

編輯:既然你不使用物理移動的汽車,你需要計算速度一些其他的方式

我建議是這樣的:

有一個腳本SpeedCalculator每輛車上的.cs。

public class SpeedCalculator : MonoBehaviour 
{ 
    public float Velocity 
    { 
     get 
     { 
      return velocity; 
     } 
    } 
    private float velocity; 

    private Vector3 lastPosition; 

    void Awake() 
    { 
     lastPosition = transform.position; 
    } 

    void Update() 
    { 
     velocity = transform.position - lastPosition; 
     lastPosition = transform.position; 
    } 
} 

然後,而不是從RigidBody圈地的速度把它從SpeedCalculator

void OnTriggerEnter(Collider c) 
{ 
    Debug.Log("GameObject is : " + gameObject.name + " and collider : " + c.name); 

    Vector3 directionB = this.transform.position - c.transform.position; 
    Vector3 directionA = c.transform.position - this.transform.position; 

    Vector3 rawVelocityA = this.GetComponent<SpeedCalculator>().Velocity; 
    Vector3 rawVelocityB = c.GetComponent<SpeedCalculator>().Velocity; 

    Vector3 realSpeedA = Vector3.Project (rawVelocityA, directionA); 
    Vector3 realSpeedB = Vector3.Project (rawVelocityB, directionB); 

    if (realSpeedA.magnitude > realSpeedB.magnitude) 
    { 
     Debug.Log ("A hit B"); 
    } 
    else if (realSpeedB.magnitude > realSpeedA.magnitude) 
    { 
     Debug.Log ("B hit A"); 
    } 
    else 
    { 
     Debug.Log ("A hit B and B hit A"); 
    } 
} 
+0

解決方案沒有工作我測試它在兩個立方體它現場查看它alwasy顯示A擊中B和B擊中A –

+0

這是令我驚訝,因爲我不知道這裏可能是錯誤的 - 爲什麼realSpeed向量是相等的。你確定rigidBody有速度嗎?我說他們沒有。我將編輯答案。 –

+0

有沒有速度,因爲我告訴你我正在通過移動方向箭頭在場景視圖中檢查它我正在移動對象僅用於檢查代碼 –

相關問題