I don't understand why OnTriggerExit2D works

In the lecture, I said that because when you reach the end, you are no longer in contact with the ground. However, the wall layer is ground.
The description of ontriggerexit2d says that it is executed when the collision ends, but in reality, the boxcollider continues to collide until it reaches the end of the ground and wall. But how does OnTriggerExit2d work?

Hi OrirO,

Welcome to our community! :slight_smile:

Actually, it’s not. Only the edge/outline is the ground collider. If you pause the game, zoom in and click the third button next to the play button, you can flip through the frames, and you’ll see that the enemy turns around when its periscope collider exists the thin green edge.

Did this clear it up for you?


See also:

1 Like

If you set the Geometry Type to “Polygons” instead of “Outlines” in the Composite Collider 2D of the tilemap, then you would indeed be right, since polygons are treaded like filled-in shapes. If you wanted to use polygon geometry, I can confirm after testing that you would need a different method to change the gomba’s direction, since it would work for ledges but not work for walls.

Outlines on the other hand are the opposite. You can only collide with the line itself, and the inside space is essentially empty.

So in this example, I think OnTriggerExit2D is behaving how you expect it to. While the gomba is walking on an open stretch of ground, the upside-down periscope (trigger collider) is halfway above and below the ground, so it intersects with the “outline”. When the gomba approaches a ledge or a wall, the trigger collider will completely clear the outline and call OnTriggerExit2D.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms