How to recognise, if OnTriggerEnter was called by Trigger on GameObject itself, or on another GameObject? Since both colliders are of different size.
For example, trigger on object A should react to objects within 1 meter area while other trigger on object B should react to objects within 2 meters area. But when 2m trigger on B gets triggered by object A 1,5m far from it, it also calls on OnTriggerEnter on A and it looks like, it detected B, but B is too far to be detected by A.
Then result looks like:
B detected A
A detected B
In my case, both triggers doing completely different stuff, while working with same objects, so I cannot just check distance, tag, name,… since both objects are identical.
I don’t know if this is the right answer, but you can create a second collider on object B and give it a tag or something. Then, when you check for collision on object A you can ignore other collider.
Hi , you should use the onCollisionEnter method, you can use several properties to make shure you can trigger the exact collider you want, for this method to work you need to set correctly the properties of the object you want to collide with... first you need to make shure that the check mark in the Collider Properties (inspector window) that says "IsTrigger" is checked ,this is so the trigger method can detect it...When this is checked objects can go thru this collider not hiting with him, so you may need to put two colliders on the object makin the "IsTrigger" one bigger than the normal one so the system can detect the collider... Next you need to decide on how you are going to detect the collision, this can be with "GameTag" "Name" or others... @Goldiedog123 gave an example using "Gametag" we will use the "Name" of the game object.
void OnCollisionEnter(Collision dataFromCollision)
{
if (dataFromCollision.gameObject.name == "GameObjectName")
{
//Do whatever you want
}
}
}
Look that when you get the data from collision is in a "Collider" type and you acces the gameObject properties of this collider...... one of the game object properties is "name" others are "CompareTag" ,"GetComponent", "GetComponentInChildren" and so on
you can also acces other properties of the collider itself
Vector3 triggerPosition = new Vector3 (1.0f,2.3f,4.3f); // Storing some position in space
void OnCollisionEnter(Collision dataFromCollision)
{
if (dataFromCollision.transform == triggerPosition){
//Not very useful but here it does something when you collide with something in this exact triggerPosition
}
}
I truly recomend you to use this method, is one of unity´s most powerful tools. you can activate a scene when we pass thru a collider or maybe make an explosion, a jump scare, a portal when we hit a door with a special tag, a respawn if you get out of level.... etc