我看到几个途径,其中这是最简单的:
编辑:注意 - RigidBidy
需要有速度,我的意思是,你需要使用AddForce
或任何其他物理运动为基础RigidBody
移动对象。
检查速度。 获取RigidBody
每辆车和检查velocity
- 一个更高的velocity
Vector3
是一个打。请注意,您需要在这里使用投影。
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");
}
}
难以承担的事情。我建议发布你的尝试,并解释你在哪里阻止。 –
添加了一些细节的代码片段 –
检查对象的速度(假设击中的速度更快)或使用更多的对撞机(只能用前一个击中) –