# How to make enemies spawn and approach player in radius

**URL:** https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387
**Category:** Help
**Tags:** game
**Created:** [June 21, 2024, 4:27pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387 "2024-06-21T16:27:37Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![GotMilk](https://avatars.discourse-cdn.com/v4/letter/g/7ab992/32.png) [@GotMilk](https://forum.makecode.com/u/GotMilk)
#### Post date: [June 21, 2024, 4:27pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/1 "2024-06-21T16:27:37Z")

</div>

hi, i’m making an adventure game and i’m trying to implement enemies. I want them to spawn at a specific area and attack the player when they get too close. I have the code for both of them and they work separately but when i put them together it doesn’t work, Please help.

Link for the game:[https://arcade.makecode.com/S06666-01683-09087-05960](https://arcade.makecode.com/S06666-01683-09087-05960)

---

<div class="post-metadata">

### Author: ![User7](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/user7/32/16035_2.png) [@User7](https://forum.makecode.com/u/User7)
#### Post date: [June 21, 2024, 6:07pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/2 "2024-06-21T18:07:58Z")

</div>

Heres a fix it doesn’t have a radius or the radius is to big but the enemies spawn and move to the player  
[https://arcade.makecode.com/S16597-31483-75401-89839](https://arcade.makecode.com/S16597-31483-75401-89839)

---

<div class="post-metadata">

### Author: ![GotMilk](https://avatars.discourse-cdn.com/v4/letter/g/7ab992/32.png) [@GotMilk](https://forum.makecode.com/u/GotMilk)
#### Post date: [June 21, 2024, 7:01pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/3 "2024-06-21T19:01:48Z")

</div>

Thanks!

---

<div class="post-metadata">

### Author: ![anon30657772](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@anon30657772](https://forum.makecode.com/u/anon30657772)
#### Post date: [June 21, 2024, 7:02pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/4 "2024-06-21T19:02:23Z")

</div>

Someone recently asked **[a similar question](https://forum.makecode.com/t/closest-player-to-an-enemie/29343)**!

You can use **[this](https://github.com/jwunderl/arcade-sprite-util)** extension which has this built-in block to get distance between sprites (and lots of other handy stuff 🤩)

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/0/0eb3866671fe8d8569f8340d82ac545cf1437d92.png)

As for your _own_ code, the reason it’s not working is because you called the radius check function only in the “On start” block! If you put it in a “Forever” block, it will work like **[this](https://arcade.makecode.com/S00391-43170-21553-31585)**!

---

<div class="post-metadata">

### Author: ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)
#### Post date: [June 21, 2024, 7:16pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/5 "2024-06-21T19:16:27Z")

</div>

**Basic follow system**

> **[SpriteFollow basic](https://arcade.makecode.com/50710-46120-75310-66510)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/0/d/0d87f55d9257ab110d208d60db21d9c91e27142d.png)  
The follow code should be on a game update block so the sprites are constantly checking if the player is near.

* * *

**More complex follow**

I like to use the distance block on the enemy sprite follow speed so it’s always close to the player

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/2/c/2c8513c73189e2836d378199e12a53233b8bab9b.png)  
and also some collision so the snakes don’t overlap each other  
The 2 number parameters control the velocity and the acceleration of the enemy sprites when they overlap with each other (how fast they collide away from each other)

> **[SpriteFollow complex](https://arcade.makecode.com/60426-32615-45723-99281)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

### Author: ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)
#### Post date: [June 21, 2024, 7:17pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/6 "2024-06-21T19:17:01Z")

</div>

@Luke you should look up boids! does a similar thing

---

<div class="post-metadata">

### Author: ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)
#### Post date: [June 21, 2024, 7:24pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/7 "2024-06-21T19:24:14Z")

</div>

i’ve heard of that! I think if i can wrap my head around vectors in blocks i might be able to give it a shot

---

<div class="post-metadata">

### Author: ![Holderify](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Holderify](https://forum.makecode.com/u/Holderify)
#### Post date: [December 11, 2024, 6:19pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/8 "2024-12-11T18:19:50Z")

</div>

How do you change the distance for when the follow event is triggered?

---

<div class="post-metadata">

### Author: ![Luke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/luke/32/32519_2.png) [@Luke](https://forum.makecode.com/u/Luke)
#### Post date: [December 11, 2024, 7:59pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/9 "2024-12-11T19:59:06Z")

</div>

if distance \> variable then set follow **speed to 0**  
variable = distance at which enemy will follow

---

<div class="post-metadata">

### Author: ![Holderify](https://avatars.discourse-cdn.com/v4/letter/h/9fc348/32.png) [@Holderify](https://forum.makecode.com/u/Holderify)
#### Post date: [December 11, 2024, 8:44pm UTC](https://forum.makecode.com/t/how-to-make-enemies-spawn-and-approach-player-in-radius/29387/10 "2024-12-11T20:44:01Z")

</div>

Thanks dude, I appreciate it .
