检查脚本相同时哪个对象与另一个对象发生碰撞 [英] Check Which object has collide with another object while scripts are same
问题描述
两个具有动画效果的汽车对象都包含刚体和盒子碰撞器,以及带有 OnTriggerEnter事件的脚本.
Two Animated car object both contains rigid-body and box collider and a script with OnTriggerEnter event.
现在,我想检查两辆车在行驶时哪辆车撞到了另一辆车.表示如果 A击中B或B击中A 都具有相同的脚本和相同的事件,则两个事件都将被触发.如何识别击中???的人?
Now I want to check that which car hit to another car while both cars are running. Means if A hit to B or B hit to A as both have same script and same event then, both event becomes trigger. How to identify it who hit ???
请记住,不建议我使用Raycast,因为它价格昂贵
例如,我制作了两个添加了方块碰撞器和刚体的立方体,以及一个包含(或选中)的脚本
For example I made two cube added box collider and rigidbody and a script that contains(or check)
void OnTriggerEnter(Collider c) {
Debug.Log("GameObject is : " + gameObject.name + " and collider : " + c.name);
}
现在,当我同时触发两个对象时,无论我将A对象击中B还是将B对象击中A,触发的顺序都保持不变.
Now when I triggering both object the order of trigger remain same no matter i hit A object to B or B object to A. How i will differentiate it?
推荐答案
我看到了几种方法,这是最简单的方法:
I see few ways, of which this is the easiest:
注意-RigidBidy
需要具有速度,我的意思是您需要使用AddForce
或任何其他基于物理的运动使用RigidBody
移动对象.
Note - RigidBidy
needs to have velocity, I mean you need to move objects with RigidBody
using AddForce
or any other physic based movement.
-
检查速度. 获取每辆汽车的
RigidBody
并检查velocity
-命中率更高的velocity
Vector3
.请注意,您需要在此处使用投影.
Checking the velocity. Getting
RigidBody
of every car and checking thevelocity
- the one with highervelocity
Vector3
is the one that hits. Note that you need to use projection here.
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");
}
}
由于您没有使用物理学来移动汽车,因此需要以其他方式计算速度
我建议这样:
每辆车上都有一个SpeedSpeedulator.cs脚本.
Have a script SpeedCalculator.cs on each car.
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
加速:
And then instead of taking speed from RigidBody
take it from 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");
}
}
这篇关于检查脚本相同时哪个对象与另一个对象发生碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!