# Projectile making problems

**URL:** <https://forum.makecode.com/t/projectile-making-problems/3071>\
**Category:** Help\
**Created:** [August 10, 2020, 2:33pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071 "2020-08-10T14:33:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nobrain](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nobrain/32/1078_2.png) [@Nobrain](https://forum.makecode.com/u/Nobrain)\
**Post date:** [August 10, 2020, 2:33pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071/1 "2020-08-10T14:33:20Z")

</div>

Well, I wanted to make an enemy (rifleDirk) shoot projectiles to “mySprite” direction every second when mySprite is in the rifleDirk’s range, but I don’t know how. And when you meet rifleDirk in the beginning, the other “snakes” won’t follow “mySprite”. There is a test tilemap level that I put every object the game will contain, so that I can test them, and that is the reason that mySprite floats in the air. ( I didn’t do the pixel art myself and a part of my code is taken by [Castle crawler](https://forum.makecode.com/t/castle-crawler/1343) )  
[https://makecode.com/\_fAp4FXaTo3Co](https://makecode.com/_fAp4FXaTo3Co)

---

<div class="post-metadata">

**Author:** ![livcheerful](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/livcheerful/32/3509_2.png) [@livcheerful](https://forum.makecode.com/u/livcheerful)\
**Post date:** [August 10, 2020, 11:45pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071/2 "2020-08-10T23:45:59Z")

</div>

I made this test code! I think it does what you want:

> **[Smart Projectile](https://arcade.makecode.com/79345-65381-79014-11412)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

There’s basically two components to it:

1. Creating a custom projectile that shoots in the direction of the hero. The easier way to it is to set the vx to be the distance of the hero from the enemy in the x direction, and vy to be the distance in the y direction. The problem is that the closer the hero is to the enemy, the slower the projectiles will be. To fix this, I added the “normalizing ratio” variable, so that no matter where the hero is, the projectile will have the same speed

2. Checking to see if the hero is in range. This is done by the distance formula

Lemme know if you have questions! The math is kind a pain, but I can explain better if you’d like to know! otherwise feel free to use this code 😃

---

<div class="post-metadata">

**Author:** ![jacob\_c](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jacob_c/32/1532_2.png) [@jacob\_c](https://forum.makecode.com/u/jacob_c)\
**Post date:** [August 11, 2020, 7:00pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071/3 "2020-08-11T19:00:46Z")

</div>

That’s awesome @livcheerful!

Another way you can solve part 1—that I think I learned from @richard on an earlier advanced stream 😁—is to use the `atan2` trig function to get the angle to the target. Once you have the angle, `cos` and `sin` can give you the x and y components (respectively) of the vector to the target… which you can scale however you like to control the speed.

I made a couple of edits to livcheerful’s solution to show that alternative approach: [https://makecode.com/\_TL8JM9i0pT6C](https://makecode.com/_TL8JM9i0pT6C) (the big change is in the `createProjectileThatFollows` function).

---

<div class="post-metadata">

**Author:** ![livcheerful](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/livcheerful/32/3509_2.png) [@livcheerful](https://forum.makecode.com/u/livcheerful)\
**Post date:** [August 11, 2020, 7:18pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071/4 "2020-08-11T19:18:49Z")

</div>

ooo love! the trig approach 😃

it seems like you gotta do some math no matter what method you use 😅

---

<div class="post-metadata">

**Author:** ![Nobrain](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nobrain/32/1078_2.png) [@Nobrain](https://forum.makecode.com/u/Nobrain)\
**Post date:** [August 14, 2020, 11:20am UTC](https://forum.makecode.com/t/projectile-making-problems/3071/5 "2020-08-14T11:20:15Z")

</div>

Thanks so much, I really appreciate it!  
Here is my fixed code if anyone wants to try:

> **[Hyper Light Drifter](https://arcade.makecode.com/74601-72789-95550-25377)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

**Author:** ![jacob\_c](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jacob_c/32/1532_2.png) [@jacob\_c](https://forum.makecode.com/u/jacob_c)\
**Post date:** [August 14, 2020, 2:57pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071/6 "2020-08-14T14:57:00Z")

</div>

Wowwwww. The visuals (character design, animation, background, etc) aren’t anything like what I expected to see in a MakeCode arcade game. Incredible!

---

<div class="post-metadata">

**Author:** ![Nobrain](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nobrain/32/1078_2.png) [@Nobrain](https://forum.makecode.com/u/Nobrain)\
**Post date:** [August 24, 2020, 9:45am UTC](https://forum.makecode.com/t/projectile-making-problems/3071/7 "2020-08-24T09:45:00Z")

</div>

Well, I used tool for converting png to javascript, and the sprites in that game were taken from the internet, from the original [Hyper Light Drifter](https://www.youtube.com/watch?v=nWufEJ1Ava0) game.

---

<div class="post-metadata">

**Author:** ![jacob\_c](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jacob_c/32/1532_2.png) [@jacob\_c](https://forum.makecode.com/u/jacob_c)\
**Post date:** [August 24, 2020, 3:00pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071/8 "2020-08-24T15:00:52Z")

</div>

Oh wow. I’ve never seen that before. Thanks for pointing it out!

---

<div class="post-metadata">

**Author:** ![Aarav\_21](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/aarav_21/32/4531_2.png) [@Aarav\_21](https://forum.makecode.com/u/Aarav_21)\
**Post date:** [July 6, 2021, 3:00am UTC](https://forum.makecode.com/t/projectile-making-problems/3071/9 "2021-07-06T03:00:00Z")

</div>

Please help me making some of the art I want to edit this game and increase it

---

<div class="post-metadata">

**Author:** ![nayrbgo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/nayrbgo/32/668_2.png) [@nayrbgo](https://forum.makecode.com/u/nayrbgo)\
**Post date:** [March 30, 2022, 6:10pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071/10 "2022-03-30T18:10:58Z")

</div>

So excellent! I love this idea. Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![Zyphfan](https://avatars.discourse-cdn.com/v4/letter/z/7ba0ec/32.png) [@Zyphfan](https://forum.makecode.com/u/Zyphfan)\
**Post date:** [December 9, 2024, 7:06pm UTC](https://forum.makecode.com/t/projectile-making-problems/3071/11 "2024-12-09T19:06:52Z")

</div>

@livcheerful How can i make the enemy projectile damage the player and the player can also shoot a projectile? I’m having some trouble in my game changing the kind of projectile to make the two projectiles different.
