# How do i make an enemy that follows you and shoots at you?

**URL:** <https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913>\
**Category:** Help\
**Created:** [May 6, 2021, 9:17pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913 "2021-05-06T21:17:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylsaster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dylsaster/32/4215_2.png) [@Dylsaster](https://forum.makecode.com/u/Dylsaster)\
**Post date:** [May 6, 2021, 9:17pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/1 "2021-05-06T21:17:42Z")

</div>

Im making a deadpool shooter and I REALLY need that code. Any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![ThunderDrop180](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/thunderdrop180/32/6820_2.png) [@ThunderDrop180](https://forum.makecode.com/u/ThunderDrop180)\
**Post date:** [May 6, 2021, 11:11pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/2 "2021-05-06T23:11:04Z")

</div>

For the chase part,  
 ![qwer](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/1aab79ddb723811e52fc4bf96063ad2dd4de642f.jpeg)  
for the shoot part, sorry but idk.  
if you want better pathfinding for the enemies, idk again.

---

<div class="post-metadata">

**Author:** ![Agent\_14](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/agent_14/32/17567_2.png) [@Agent\_14](https://forum.makecode.com/u/Agent_14)\
**Post date:** [May 7, 2021, 12:36am UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/3 "2021-05-07T00:36:06Z")

</div>

Use the sprite data extension to throw projectiles.

---

<div class="post-metadata">

**Author:** ![ThunderDrop180](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/thunderdrop180/32/6820_2.png) [@ThunderDrop180](https://forum.makecode.com/u/ThunderDrop180)\
**Post date:** [May 7, 2021, 2:00am UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/4 "2021-05-07T02:00:21Z")

</div>

Thanks. i had the same problem.

---

<div class="post-metadata">

**Author:** ![girlwhocode](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/girlwhocode/32/3754_2.png) [@girlwhocode](https://forum.makecode.com/u/girlwhocode)\
**Post date:** [May 7, 2021, 10:17pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/5 "2021-05-07T22:17:57Z")

</div>

can you send the link to what game you’re making please and thank you

---

<div class="post-metadata">

**Author:** ![Dylsaster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dylsaster/32/4215_2.png) [@Dylsaster](https://forum.makecode.com/u/Dylsaster)\
**Post date:** [May 10, 2021, 5:59pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/6 "2021-05-10T17:59:41Z")

</div>

> **[Deadpool game](https://arcade.makecode.com/51176-62180-59095-63751)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

**Author:** ![Dylsaster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dylsaster/32/4215_2.png) [@Dylsaster](https://forum.makecode.com/u/Dylsaster)\
**Post date:** [May 14, 2021, 5:32pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/7 "2021-05-14T17:32:06Z")

</div>

A quick update: The enemies dont shoot but I have updated the game. There is a working katana and cutscenes. There’s a cutscene when you die and if you reach 55 score [https://makecode.com/\_i4VhjzfkxPVf](https://makecode.com/_i4VhjzfkxPVf)

---

<div class="post-metadata">

**Author:** ![jerome](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jerome](https://forum.makecode.com/u/jerome)\
**Post date:** [June 1, 2023, 8:34pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/8 "2023-06-01T20:34:05Z")

</div>

I havd the exact same problem here is my game if you want to see some code [https://arcade.makecode.com/S71414-38650-97092-96697](https://arcade.makecode.com/S71414-38650-97092-96697)

---

<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:** [June 5, 2023, 5:12pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/9 "2023-06-05T17:12:50Z")

</div>

over all i have a good solution for all of you  
im makecode arcade there is a squareRoot() block  
well in that block there is sin, cos and atan2  
what we need to do is find the radians between two points using atan2 and make that into a velocity multiplier using cos and sin

how do we do that? well to find the radian between two points use somthing like this

```auto
dx = x2 - x1
dy = y2 - y1
radians = Math.atan2(dy, dx)

```

or

```auto

radians = Math.atan2(y2 - y1, x2 - x1)

```

then to set the speed of your projectile use sin and cos

```auto
mySprite.setVelocity(cos(radians) * speed, sin(radians) * speed)

```

it looks something like this  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/f/fac671e0c6c31aa82b76cc148f1cb1b0209bc7b7.png)

---

<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:** [June 8, 2023, 11:23pm UTC](https://forum.makecode.com/t/how-do-i-make-an-enemy-that-follows-you-and-shoots-at-you/7913/10 "2023-06-08T23:23:38Z")

</div>

sorry i forgot to put sin and cos  
cos is for the vx and sin is for vy (sin/cos of the radians)  
And in dy dont put (sprite).x - (value).x put (sprite).y - (value).y
