I am making a game where I have a player and enemies that depending on the distance, follow the player.

For enemies to follow the player, the map was marked with a grid. When the player approaches the enemy, the enemy creates a path to the player, ignoring the walls (obj_Block).

Code - Creation grid (obj_ADM):



Code - Creation path (obj_Enemy):

xPlayer=(obj_Player.x div obj_ADM.celLar)*obj_ADM.celLar+obj_ADM.celLar/2;
yPlayer=(obj_Player.y div obj_ADM.celAlt)*obj_ADM.celAlt+obj_ADM.celAlt/2;

  • The grid is 20x20.

  • The size of the room tiles division is also 20x20:

enter image description here

  • The obj_Block size is also 20x20.

  • But the size of the enemy is 32x32.

So, something similar to the image below happens:

enter image description here

  • Purple Square => enemy
  • Black Squares => wall

The enemy ends up going through a part of the wall while chasing the player.

This is obviously because of the different size of the grid and the enemy. Because I did a test with the enemy with the size of 20x20 and it worked perfectly.

I changed the size of the grid (which creates the path) to 32x32, and it turned out that instead of crossing the wall, the enemy ends up being well away from the walls (in some places on the map):

enter image description here

I would like to know how to do so that none of these situations happens. Regardless of the size of the enemy.

Video I used to make the code:



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.