- Home /
Is it possible to detect different colliders with one script ?
I want to handle in a single object different Colliders (Triggers). If an object enters in one of the colliders, I need to know in which one. Is that possible ?
Answer by UnityCoach · Jul 31, 2018 at 01:38 PM
The way it works is that it's the triggers that receive the messages. So you need to forward the message to the object entering the triggers. Something in the like :
// on the Trigger
void OnTriggerEnter (Collider other)
{
other.gameObject.SendMessage ("Entered", this.gameObject);
}
// on the entering object
void Entered (GameObject trigger)
{
// do what you need with the given object
}
Now, using SendMessage isn't the best option performance wise, but it's enough for prototyping.
You can also use component or interface
// on the Trigger
void OnTriggerEnter (Collider other)
{
other.GetComponent<Player>.Entered (this.gameObject);
}
// on the entering object
public void Entered (GameObject trigger)
{
// do what you need with the given object
}
Your answer
Follow this Question
Related Questions
Check for collision while animating 0 Answers
OnTriggerStay with multiple collision parameters 1 Answer
Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer
Animation with frozen player ?? 0 Answers
On Trigger Enter, Collide with object, specific collision 1 Answer