0
votes

I am using unity 5 c# and I have a gameobject with 2 trigger colliders one of them is in a different location. I need to be able to use OnTriggerStay2D and OnTriggerEnter2D for them but I need to find what trigger is being entered. Right now if I enter the 1st(polygon) trigger the OnTriggerEnter activates for the 2nd(box). How can I Tell the two colliders apart???

public void OnTriggerEnter2D(Collider2D other) //2nd collider trigger
{
    if (other.tag == "Player") {
        Found = true; //if the player is in shooting range
        Idle = false; 
    } 
}
public void OnTriggerStay2D(Collider2D other) //1st collider trigger
{
        if (Found != true) {
            if (other.tag == "Player") {
                Shield = true;
                Idle = false;
            } 
    }
}

public void OnTriggerExit2D(Collider2D other) //2nd collider trigger
{
    if (other.tag == "Player") {
        Found = false;
        Shield = false;
        Shooting = false;
        Idle = true;
    } 
}

I have tried making the 1st trigger public void OnTriggerStay2D(PolygonCollider2D other) but it says "This message parameter has to be of type: Collider2D The message will be ignored."

What I am trying to do is have a polygon trigger in front of the gameobject and a different box trigger closer to the gameobject so when you go near the gameobject you enter the 1st trigger and it puts its shield up but when you get close to it (within shooting range of it) it will put its shield down and start shooting you.

2

2 Answers

0
votes

Well collider2d detects all types of 2d colliders. It doesn't matter if it's polygon or just a box. As the documentation suggestions it doesn't need to be public or private. It only takes a collider2d as it's argument however.

For debugging purposes why not use print?

Print("you've entered the trigger function");

Also I wouldn't use 2 different trigger colliders on the same GameObject. Why not just make 2 separate gameobjects so you can have more thorough detection. Each GameObject with its own trigger collider can have different tags.

If you have to use 2 trigger colliders on one object. Which isn't the best idea. You could use shapeCount to determine which one it's hitting. Although like I said I would warrant against doing 2 trigger colliders on the same object when whatever you're trying to do can be easier on two separate objects.

However links aren't usually prohibited I think. I would watch and study these videos. They're very useful for explaining the engine and they really aren't even that long. https://unity3d.com/learn/tutorials/modules/beginner/2d They even have a video explaining 2d colliders.

0
votes

This is my fix. In one of my games I have a boulder, I have a trigger which will delete a block below it so it falls, I then have another trigger which tells the boulder to start moving left or right I then also have another trigger which will delete the boulder once the boulder comes in contact.

So what you can do is create 2 new game objects, create a new CS file and name them appropriately, then with those two new classes allow them to take in the gameobject you are referring to in your question.

Then when they are triggered you can use code from their class.

So your first class would become something like this

public void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") {
        Enemy.Found = true; //if the player is in shooting range
        Enemy.Idle = false; 
    } 
}

public void OnTriggerExit2D(Collider2D other)
{
    if (other.tag == "Player") {
        Enemy.Exit2DTrigger();
    } 
}

Then the other class would be something like this

public void OnTriggerStay2D(Collider2D other)
{
        if (Enemy.Found != true) {
            if (other.tag == "Player") {
                Enemy.Shield = true;
                IEnemy.dle = false;
            } 
    }
}

public void OnTriggerExit2D(Collider2D other)
{
    if (other.tag == "Player") {
        Enemy.Exit2DTrigger();
    } 
}

Then in your Enemy class you would have

public void Exit2DTrigger()
{
    Found = false;
    Shield = false;
    Shooting = false;
    Idle = true;
}

P.S. also don't you need to use other.gameObject.tag == "Player" ?