This site requires JavaScript, please enable it in your browser!
Greenfoot back
Bananarama
Bananarama wrote ...

2018/7/23

Tracking and chaseing player

Bananarama Bananarama

2018/7/23

#
Hey i am currently making a game where enemies will chase a player through a maze and i am having trouble with figuring out how to get the enemies to track and chase the player whilst not going through the walls I found this discussion (www.greenfoot.org/topics/58769/0 ) about the tracking part and looks like it will work great but does anyone know how to get the enemies to follow the path (not go through the walls) Thanks
Super_Hippo Super_Hippo

2018/7/23

#
Check for walls in the four directions. If there is only one option available, choose this option to walk there. If there are two options, use the one where you didn't come from. If there are more than two options, choose a random one (or the one nearest to the target) maybe also excluding the one you came from (up to you).
Bananarama Bananarama

2018/7/23

#
thats a good idea lol i dont know why i didnt think to do that do u know the best way to detect the direction thats closest to the player (the most direct route)?
Super_Hippo Super_Hippo

2018/7/23

#
Use the 'Math.hypot' method (Pythagoras) to check the distances if you moved to the direction. So use the player's location and the current location of the enemy ± some x/y value depending on the direction you check.
You need to login to post a reply.