4
\$\begingroup\$

Two animated car object both contain a rigid body, a box collider and a script with an OnTriggerEnter event.

I want to check which car started the collision event, by hitting the other car.

Consider two cars; regardless of whether carA hits carB or carB hits carA, both cars contain the same script with the same event. So both cars become the trigger.

How do I identify which car started the collision? I wish to do so without the use of raycasting, as it is too expensive for what I am wanting to do.

\$\endgroup\$
1
  • \$\begingroup\$ Took me a bit to understand what you actually mean. It sounds like your asking "check which objects they are" not "check which one started it". I can see the confusion, in the answers.. \$\endgroup\$
    – Gnemlock
    Commented Aug 20, 2016 at 1:05

5 Answers 5

1
\$\begingroup\$

You can find out the tag or name of the objects that collided, this information is stored in the Collider other you pass to the OnTriggerEnter(Collider other) function.

Deciding who hit who is something you need to set the criteria for.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ this is not the answer \$\endgroup\$ Commented Jun 13, 2016 at 8:50
  • \$\begingroup\$ I agree; I initially upvoted this answer thinking it was obviously correct. I believe the answerer made the same mistake I had when reading the question, and misinterpreted it. The actual question asks 'how to figure out who hit who', which this answer appears to address only by repeating that it is '.. something you need to set a criteria for'. \$\endgroup\$
    – Gnemlock
    Commented Aug 20, 2016 at 3:00
1
\$\begingroup\$

You could create a boolean, for example bool canCrash, and make sure it is true when ever the car changes from the previous position. Then, on collision, you check if canCrash == true and, if it isn't but the speed of the car was higher than the car it collided into.

I think that's the right direction to go.

\$\endgroup\$
1
  • \$\begingroup\$ i want to tell you that if two object trigger their trigger event fired abruptly. \$\endgroup\$ Commented Jul 15, 2016 at 5:29
0
\$\begingroup\$

Playing around collision and the Debug.Log(), I have confirmed that OnTriggerEnter triggers in a specific order; that is, when carA moves into carB, it will set off the OnTriggerEnter method attached to carB. This always seems to happen first, with the OnTriggerEvent method attached to carA triggering, second.

Working off of the answer submitted by Vadim Tatarnikov, I set up bool collisonInstigator that typically defaults as false. I was than able to check for collisionInstigator == true during OnTriggerEnter. If collisionInstigator == false, we assume that the other collider is the instigator, and set other.<script>.collisionInstigator = true. When the instigators OnTrigger method calls, which we can assume will be directly after the first, we find that collisionInstigator == true.

This tells us that we are currently in the OnTrigger method for the car that started the collision, allowing us to run our logic as required. The only other thing we have to do is ensure that we set collisionInstigator = false at the end of our logic, to set it back up for the next potential collision scenario, assuming your not calling for the instigator to be destroyed or any other logic that would make further collisions with that game object impossible.

public class CarAI : MonoBehaviour
{
    public bool collisionInstigator = false;

    void OnTriggerEnter(Collider other)
    {
        if(collisionInstigator == true)
        {
            // I am crashing into the other car; run logic

            DebugCollision(other.gameObject);   // confirm order through debug
            collisionInstigator = false;        // don't forget to reset the flag!
        }
        else
        {
            // I am being crashed into by the other car
            other.GetComponent<CarAI>().collisionInstigator = true; 
        }
    }

    ///<summary>Using tags, we can double check our logic regarding which GameObject
    /// is the "instigator"</summary>
    void DebugCollision(GameObject other)
    {
        string myName = gameObject.name;
        string otherName = other.gameObject.name;

        Debug.Log(myName + " is colliding with " + otherName);
    }
}

Using DebugCollision(GameObject other) to debug a clearer formatted string of the collision, I confirmed that the above script will correctly pick the "instigator" across a variety of speeds, using both Transform.Translate() and RigidBody.AddForce() methods.

\$\endgroup\$
0
\$\begingroup\$

You can check the speed of both cars

During collision, there will be two cases:

  1. One car is moving, one car is stationary.
  2. Both cars are moving.

You need to decide which car intentionally hit the other, and obviously, OnTriggerEnter() will not help. To check for speed, use GetComponent<Rigidbody2D>().velocity or GetComponent<Rigidbody>().velocity.

case one - one car is stationary

In the first case, it's pretty simple. The car that has speed is the car that is hitting the other car.

case two - both cars are moving

It's tricky for the second case. You can say the car with more speed intentionally hit other car; though it's not always true, it will work most of the time.

\$\endgroup\$
0
-1
\$\begingroup\$

I think you'd be more lucky to use OnCollisionEnter to avoid a double event.

You could also combine your condition with a second criteria, deciding which is the "collider" and which is "collided" (depending on your game context). For instance, you could check for the fastest car, the car that is changing direction, the car with the most on the outside.. there would be a behavior for the "collider", and a behavior for the "collidee".

You could also tweak the collision event with tag name identification; if the tags of both cars are "AI cars", treat them as a bounce, only. If one tag is "player car", treat it as a real shock.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ what are you saying man complete not understand \$\endgroup\$ Commented Jun 13, 2016 at 9:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .