2016-06-13 59 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

有没有速度,因为我告诉你我正在通过移动方向箭头在场景视图中检查它我正在移动对象仅用于检查代码 –

相关问题