Simple way to do "Aimed shots - spreadshots"?

Hello. I’ve started using GDevelop a few days a ago and though I’d try porting a game from Game Maker to GDevelop.

GDevelop allows to make aimed bullets to target enemies or the player. I’m looking for a way to do aimed spreadshots. This is a really easy concept but I find it hard to explain, so here is a picture:
Situation 1 I can do easily as this is just shooting straight down at an angle of 90 degree, the other two bullets have an angle of 110° and 70°.
Situation 2 is what I can’t figure out. It’s the same spread pattern as before, but it dynamically adjusts to the players position.

I have a bit of Game Maker background knowledge (from the Game Maker 8.1 days). There I’ve solved the problem like this

shot = instance_create(x+30,y+20,obj_bullet1) with shot move_towards_point(global.shipx,global.shipy,2.2) shot = instance_create(x+30,y+20,obj_bullet1) with shot move_towards_point(global.shipx,global.shipy,2.2) shot.direction += 20 shot = instance_create(x+30,y+20,obj_bullet1) with shot move_towards_point(global.shipx,global.shipy,2.2) shot.direction -= 20
So basically this creates the bullet and tells it to move towards the player. For the bullets on the left and right, the angle is modified by adding 20° or substracting 20°. This is what I can’t do in GDevelop.

I’ve looked for a formula that gives the the angle between enemy ship and player ship, but this is still buggy (doesn’t work when player’s x position is smaller than enemy’s x position) and pretty complicated. I’d rather have an easier solution than this.

The “atan2” function was made to avoid sign problems respect to the points positions, this function does some inner checks to get the right angle, so you shouldn’t have problems at all. But I can’t find what is the problem with your code, here is an example I made, test it and compare with your code to find the error:
There are little differences in this example: Instead a timer, you can fire the bullets with the Spacebar, another difference is that I save the angle in a variable and use this angle variable for the three bullets instead recalculate the same angle over and over again.

Finally, you can share the project here (the .gdg file plus some working images), so we can debug it :slight_smile: (4.02 KB)

Thank you very much, this is just what I have been looking for.
I’ve been implementing this, including the method of using a precaluclated variable instead of multiple angle calculations, and it works fine.

I’ve also found out why it didn’t work in my case. I’ve been using this instruction here: … ontal-axis

This mentions both arctan and atan2. With arctan (called atan in GDevelop) this won’t work properly depending on the player position, so I’ve inserted conditions to use different forumlas depending on the player position. That didn’t work so well. When testing atan2 instead of atan, I simply forgot to remove these conditions. That was… not very smart, sorry.

Anyway, thanks a lot again.
Also good to know that there are a few extra functions ( … s/built_in)
The “ToDeg” function seems a better and more precise way to get angles in degrees instead of multiplying with “180/3.14”.

Good to know you get it to work :slight_smile:

I noticed the extra position conditions not needed with atan2, but I thought you added them because you were getting problems :stuck_out_tongue:

Surely, but you will not notice any difference if you add some decimals of PI, as 3.15169 :wink: