# Help with Firing a Cannon in a game

**URL:** <https://forum.code.org/t/help-with-firing-a-cannon-in-a-game/41824>\
**Category:** Coding and Debugging Help\
**Created:** [February 27, 2026, 1:23pm UTC](https://forum.code.org/t/help-with-firing-a-cannon-in-a-game/41824 "2026-02-27T13:23:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flynn\_Lives](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/flynn_lives/32/9762_2.png) [@Flynn\_Lives](https://forum.code.org/u/Flynn_Lives)\
**Post date:** [February 27, 2026, 1:23pm UTC](https://forum.code.org/t/help-with-firing-a-cannon-in-a-game/41824/1 "2026-02-27T13:23:40Z")

</div>

**Link to the project or level:** [Game Lab - Code.org](https://studio.code.org/projects/gamelab/9639f54c-50f4-4709-8fd4-3643600c373d)

**What I expect to happen:** The cannon (attatchment – spelling, I know 🙂 ) should fire the projectiles at the same angle that the cannon is positioned in.

**What actually happens:** It only fires sometimes at some angles and when it’s fired, it doesn’t fire at the same angle as the cannon

**What I’ve tried:** Not much–wasn’t sure how to handle this one!

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [February 27, 2026, 3:42pm UTC](https://forum.code.org/t/help-with-firing-a-cannon-in-a-game/41824/2 "2026-02-27T15:42:20Z")

</div>

Good morning!

I took a quick look and although I see where the orientation of the cannon and the projectile are changed, it’s the velocityX and velocityY that you are using to move the object and those are always hard-coded with a number, so you will want to see if you can figure out how to tie the orientation of the object into the velocity in a variable manner in order to change it.

Also, the reason it sometimes fires and sometimes doesn’t is coming from your conditional on line 47. The firing only works if the cannon is oriented between 0 and 90 degrees.

If this doesn’t help you or doesn’t quite answer the question you had, feel free to check back in!

thanks and good luck!

Mike

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [February 27, 2026, 4:09pm UTC](https://forum.code.org/t/help-with-firing-a-cannon-in-a-game/41824/3 "2026-02-27T16:09:13Z")

</div>

I did just find another question in the forum along the same lines as yours. The replies to this post may help further.

> [@Sprite direction-Asteroids: Game Lab](https://forum.code.org/t/sprite-direction-asteroids-game-lab/38879/3):
>
> Hi- Few notes since: seems only the animation rotates w/ rotationspeed, and not the sprite itself-stays facing same direction, which is what I’m trying to work around. Make sprite rotate w/ the animation…thx for the hand Here’s the link: [Game Lab - Code.org](https://studio.code.org/projects/gamelab/iXO4mHSrcJVbMFb-2to5JiCXKgP-tMmJUPXWRZ4sKyw)

---

<div class="post-metadata">

**Author:** ![pluto](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/pluto/32/9141_2.png) [@pluto](https://forum.code.org/u/pluto)\
**Post date:** [February 27, 2026, 8:33pm UTC](https://forum.code.org/t/help-with-firing-a-cannon-in-a-game/41824/4 "2026-02-27T20:33:30Z")

</div>

Greetings @Flynn_Lives

I’m here to elaborate on what mwood said. He was right about his first post, however I do think I need to explain his second post.

I believe you’re trying to shoot the missile in the direction of the attachment’s direction. This is easily done with the setSpeedAndDirection() block.

```auto
missile.setSpeedAndDirection(5, attatchment.rotation+180); //.rotation is basically the direction of the attachment

```

The speed can be adjustable to your liking. If you’re wondering why we add +180, it’s because the missile actually fires in the opposite direction of the attachment, so we reverse it.

Also a thing you’d probably want to fix is the cannon going in all sorts of directions, so code it in a way to keep it fixed if it goes over a certain angle:

```auto
if (attatchment.rotation > 15) {attatchment.rotation = 15;}
if (attatchment.rotation < -90) {attatchment.rotation = -90;} //should be self explanatory

```

Lastly, if you need help with creating the missile sprites efficiently, feel free to ask.

---

<div class="post-metadata">

**Author:** ![Flynn\_Lives](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/flynn_lives/32/9762_2.png) [@Flynn\_Lives](https://forum.code.org/u/Flynn_Lives)\
**Post date:** [March 4, 2026, 3:17am UTC](https://forum.code.org/t/help-with-firing-a-cannon-in-a-game/41824/5 "2026-03-04T03:17:32Z")

</div>

Thank you! I appreciate the help.
