- Home /
How can I detect multiple triggers on same gameobject at once?
Let me elaborate this!
I'm working on a game where my player collides with multiple triggers(gameobjects with triggers) at once.Now I want to know which gameobjects are they, using tags!! How can I do that?
Answer by lyons5609 · Sep 25, 2019 at 06:34 PM
I would suggest taking advantage of the IsTouching method on Collider2D. here is an example of two triggers on one gameobject:
private BoxCollider2D sideTrigger;
private BoxCollider2D jumpTrigger;
void Start()
{
var colliders = GetComponents<BoxCollider2D>();
jumpTrigger = colliders[0];
sideTrigger = colliders[1];
}
private void OnTriggerEnter2D(Collider2D collider)
{
if (collider.IsTouching(sideTrigger))
{
Debug.Log("side");
}
else if (collider.IsTouching(jumpTrigger))
{
Debug.Log("jump");
}
}
Answer by Dragate · Oct 09, 2017 at 08:50 AM
Add this to a script on your player:
void OnTriggerEnter(Collider other) {
if(other.tag == "something"){
//stuff to do...
} else if(other.tag == "something else"){
//other stuff to do...
}
}
Already tried this!! But it's colliding with multiple triggers at a time
Answer by Morice1999 · Oct 09, 2017 at 11:40 AM
Dont know how to do that with the script on the player but then you put this on every trigger object and check the matching field in the inspector it has the same result i think.
public bool trigger1;
public bool trigger2;
public bool trigger3;
public bool trigger4;
private bool inTrigger;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
inTrigger = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
inTrigger = false;
}
}
void Update()
{
if (inTrigger)
{
if (trigger1)
{
//DO SOMETHING FOR TAG1
}
else if (trigger2)
{
//DO SOMETHING FOR TAG2
}
else if (trigger3)
{
//DO SOMETHING FOR TAG3
}
else if (trigger4)
{
//DO SOMETHING FOR TAG4
}
}
}