Platformer. Enemy Vision

Colleagues. I’m making a platformer. I want the enemy not to run after the player if the player is on a platform that is higher or lower. I did it using Y Position. But it doesn’t work for all enemies. Help!

Hello, Geldwyr!

When it doesn’t work for all enemies, what specifically happens? Some never follow? Or do they never stop following? Is Enemies a group? If so, are all your enemies included in it?

Where is the event that you are changing the state of Enemies making them stop following the player?

Use raycasting.

You can use this to cast a ray from the enemy’s position in the direction they’re looking. The ray can target the player sprite, so if the player comes into the enemy’s field of vision, the enemy will start following them. It also allows you to limit the enemy’s vision to a certain distance, preventing distant enemies from running towards your character. Even better, you can make sure the enemy can’t see through walls.

To achieve this (not seeing trough walls), group all the “solid” elements like walls and floors along with the player. Then, make the ray search for elements within that group. If the ray hits a wall, nothing happens, but if it hits the player, you can make the enemy respond accordingly.

To differentiate between objects the ray encounters within the group, create a variable for the player, such as ‘id = player’. If the encountered object has a variable ‘id’ with the value ‘player’, then make that enemy chase the player. It’s not necessary to add the ‘id’ object variable to the other elements of the group.

Before attempting this, I recommend checking out various raycasting examples available in the GDevelop repositories.

1 Like

Thanks a lot!) I’ll use raycast

1 Like