# Multiplayer enemy pathfinding

**URL:** <https://forum.makecode.com/t/multiplayer-enemy-pathfinding/22173>\
**Category:** Help\
**Tags:** game\
**Created:** [September 1, 2023, 4:23pm UTC](https://forum.makecode.com/t/multiplayer-enemy-pathfinding/22173 "2023-09-01T16:23:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FourBacon](https://avatars.discourse-cdn.com/v4/letter/f/48db29/32.png) [@FourBacon](https://forum.makecode.com/u/FourBacon)\
**Post date:** [September 1, 2023, 4:23pm UTC](https://forum.makecode.com/t/multiplayer-enemy-pathfinding/22173/1 "2023-09-01T16:23:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KIKIvsIT](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kikivsit/32/10492_2.png) [@KIKIvsIT](https://forum.makecode.com/u/KIKIvsIT)\
**Post date:** [September 1, 2023, 4:32pm UTC](https://forum.makecode.com/t/multiplayer-enemy-pathfinding/22173/2 "2023-09-01T16:32:46Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![FourBacon](https://avatars.discourse-cdn.com/v4/letter/f/48db29/32.png) [@FourBacon](https://forum.makecode.com/u/FourBacon)\
**Post date:** [September 1, 2023, 4:56pm UTC](https://forum.makecode.com/t/multiplayer-enemy-pathfinding/22173/3 "2023-09-01T16:56:49Z")

</div>

Thank you so much, I will try it right now.

---

<div class="post-metadata">

**Author:** ![YuHayate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yuhayate/32/8275_2.png) [@YuHayate](https://forum.makecode.com/u/YuHayate)\
**Post date:** [September 5, 2023, 3:18pm UTC](https://forum.makecode.com/t/multiplayer-enemy-pathfinding/22173/4 "2023-09-05T15:18:28Z")

</div>

if you dont know how to find the distence between two sprites here is how you can do it

```auto
let dist = Math.Sqrt((x1 - x2) **2 + (y1 - y2)** 2)

```

x1,y1 is the enemy and x2,y2 is the player

---

<div class="post-metadata">

**Author:** ![Sarge](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/sarge/32/19590_2.png) [@Sarge](https://forum.makecode.com/u/Sarge)\
**Post date:** [September 11, 2023, 6:05pm UTC](https://forum.makecode.com/t/multiplayer-enemy-pathfinding/22173/5 "2023-09-11T18:05:38Z")

</div>

That’s already been detailed, just using Joey’s extension, which is easier since it’s just an abstraction of that equation

---

<div class="post-metadata">

**Author:** ![YuHayate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yuhayate/32/8275_2.png) [@YuHayate](https://forum.makecode.com/u/YuHayate)\
**Post date:** [September 11, 2023, 9:20pm UTC](https://forum.makecode.com/t/multiplayer-enemy-pathfinding/22173/6 "2023-09-11T21:20:31Z")

</div>

> [@Sarge](#):
>
> already been detailed

oh… i didn’t know
