Check that a player is in an area for a certain amount of time

Hello! I tried searching for this and couldn’t find anything. That could, however, be because I’m not sure how to put it into words. I am making a 2D platformer game and I need to check if a player is on a platform for a certain amount of time. If the player stands on one platform for a duration of 2 seconds, I want to spawn an enemy there, but if the player moves before the 2 seconds are up, I don’t want the enemy to spawn. How would I do this?

When the player collides with the platform, set a timer (such as an object timer on the platform) for 2 sec. If the player is no longer in collision, delete the timer, but if timer> 2sec , spawn your enemies. That should accomplish what you need :slight_smile:

1 Like

That worked perfectly! Thank you so much!

1 Like

Will it matter if the player jumps a lot? And then the timer would keep re-setting? If so, you could have a hidden object above the platform and use that for the collision condition instead.

Instead of adding an object to each platform. I would stick a hidden rectangle (the height of it’s jump) under the player with the sticker behavior. You can use it to check for collision with a platform. You could always asign a variable to the platforms to determine whether a platform should have an enemy or a specific enemy type.

Alternatively, you could keep track of the last and current platform by ID or location (if it doesn’t move) or some other metric. Start or restart a timer each time the 2 values don’t match.