检查脚本相同时哪个对象与另一个对象发生碰撞 [英] Check Which object has collide with another object while scripts are same

查看:89
本文介绍了检查脚本相同时哪个对象与另一个对象发生碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个具有动画效果的汽车对象都包含刚体盒子碰撞器,以及带有 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.

  1. 检查速度. 获取每辆汽车的RigidBody并检查velocity-命中率更高的velocity Vector3.请注意,您需要在此处使用投影.

  1. Checking the velocity. Getting RigidBody of every car and checking the velocity - the one with higher velocity 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆