Help with tracking projectiles

Looks like your problem is here:


The problem you are having is with those “after” blocks. The program loops through every enemy, creating a hitbox sprite for each one, and then after 100 milliseconds it makes each sprite shoot a bullet with the velocity of the hitbox sprite. The problem is that the variable “hitbox” only holds the most recent hitbox sprite created, so the hitbox that every bullet uses to set velocity is just the most recent one created, which is why they all have the same velocity!

What you need to do instead is use math to determine the velocity to give the projectile, or use an extension like arcade-sprite-util to do it for you, which has a “set (sprite) velocity to (number) at angle (number)” block and a “get angle between (sprite) and (sprite)” block, which can be combined to send a projectile from one sprite towards another!

You could also look at this very long explanation that I wrote here, which has the math you would need if you didn’t use the sprite util extension: Enemies fire projectiles at me Raycasting - #2 by WoofWoof

To get the sprite util extension, search “riknoll/arcade-sprite-util” in the extension search bar.
Riknoll is the GitHub account of the moderator who made the extension, Richard!

1 Like