# Check which object triggered another

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.

• 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.. Aug 20, 2016 at 1:05

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.

• this is not the answer Jun 13, 2016 at 8:50
• 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'. Aug 20, 2016 at 3:00

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.

• i want to tell you that if two object trigger their trigger event fired abruptly. Jul 15, 2016 at 5:29

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.

## 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.

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.

• what are you saying man complete not understand Jun 13, 2016 at 9:58