I need help with a game. There is an enemy that follows one player, but I want it to follow the closest player. This is a multiplayer game with 4 players. Is there a way to do this? There are no boundaries or limitations (like collisions) so you don’t need code collisions.
1 Like
You should be able to use a forever loop and nested for loops to have each sprite of kind Enemy check the distance of each sprite of kind Player, then follow the one that’s closest. To do that, you may want to use the Sprite-Util extension (https://github.com/jwunderl/arcade-sprite-util) and you’ll probably need a variable that holds the closest distance as you iterate through each Player.
Hope that helps!
~Kiki
1 Like
Thank you so much, I will try it right now.
if you dont know how to find the distence between two sprites here is how you can do it
let dist = Math.Sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
x1,y1 is the enemy and x2,y2 is the player
1 Like
That’s already been detailed, just using Joey’s extension, which is easier since it’s just an abstraction of that equation
1 Like
oh… i didn’t know