Can't get collider trigger to work.

I’ve got a very basic setup:
I have a player object with a movement script. Inside that script is a boolean, which I want to turn on and off, based on whether the player is inside a trigger collider or not.

So, I have an object in the scene, with both a BoxCollider2D, and an EdgeCollider2D. The box collider is the trigger, and the edge one makes it so that the player can actually still collide with the top surface of that object (I have the radius of the trigger collider increased, so it reaches outside the object).

And I have no idea what to do now. I have a script for controlling the boolean based on “OnTriggerEnter2D” and “OnTriggerExit2D”. I tried putting an if statement into each, checking for actual collision with a tagged object (the player). And I also tried just updating the boolean inside those methods, without any if statements

Neither of which have worked so far.

What am I doing wrong?

Here’s the script for the trigger:

    public Movement playerMovement;

    void onTriggerEnter2D () {
        playerMovement.OnStairs = true;
    }

    void onTriggerExit2D () {
        playerMovement.OnStairs = false;
    }
}

It’s attached to the object with the trigger collider (BoxCollider2D), and has a reference to the player’s movement script (which I assigned in the inspector).

I haven’t worked with triggers ever (as far as I remember), so I have no clue what I’m doing. I watched multiple videos, but none of them helped me make it work. I had stuff in the brackets, but removed it since nothing I did worked.

For the if statements checking for collision, I had “Collider2D colide” inside the method brackets, and for just the boolean changes, I had “Movement OnStep”.

The if statement was the following:

        if (colide.gameObject.tag == "Player")

First of all, the methods need a capital O.
Secondly, the “stuff in the brackets” aka parameters is required.

Your methods should look like:

void OnTriggerEnter2D(Collider2D collision)
{
    // do something
}

void OnTriggerExit2D(Collider2D collision)
{
    // do something
}

Also make sure you’ve got a Rigidbody2D on at least one of your objects.

1 Like

Make sure to add a rigidbody 2d to one of the colliding gameobjects.

1 Like

Thanks!:slight_smile:

It’s just past midnight over here, so my brain isn’t at 100% processing power. I tend to miss stuff like this when I work at night. :sweat_smile:

And my script is mostly based around using the rigidbody’s velocity, so I wouldn’t be able to even move without a rigidbody. :wink: