# [Solved] Bullets shooting at target and center bullet spread

Greetings,

I’m making a POC which tests bullet patterns, and I’ve run into two problems.

1. I’m trying to make it adjustable to which target the bullets shoot toward, in this case, I want them to always shoot towards my mouse cursor. This is what I have:

However, the bullets absolutely refuse to work with me, and they never aim for the mouse. Is there something I missed?

1. I made the formula adjustable to make it possible to shoot multiple bullets in a spread out fan (With adjustable values to bullet spread, bullet amount, etc.)

Unfortunately, the angles of the spread are a little… off.

See, the bullets will only fire in the direction relevant to the FIRST bullet in the fan, rather than the middle of the fan aiming for that angle. For example, the guy in the middle has his bullet amount set to 5, and his firing angle set to 0, and only the bullet at the end of the fan is firing towards angle 0:

So I decided to try and remedy this by adding this extra math to the angle code:

Basically, I want the spread between the bullets to be multiplied by how many bullets are being fired out (In this case, spread of 30 with 5 bullets in the fan = 150), then cut the end result in half (75), then subtract that number from the current angle, to try and ensure that the FAN aims for the angle, not the first bullet spawned.

Here’s the result:

It’s better, but the angle is still a little off. In short, I would like the middle bullet to be shot at angle 0 (Straight to the right).

Can somebody help me out with the nitty gritty, please? I’d greatly appreciate your help ^^

aimTarget will be a string, and won’t get evaluated by GDevelop.

Ohh, that’s why I kept getting errors when writing it

I modified the instance variable to be a integer variable, and changed the code to look like this:

The bullets still don’t aim towards the mouse though

No, I think you’ve misunderstood. Setting a number variable to “Mouse.X() + Mouse.Y()” won’t work. GDevelop doesn’t evaluate that at run time, and will result in rubbish being returned as the value.

You need to set guy.angle to the angle from guy to the mouse - use the AngleBetweenObjects(…).

I’m sorry, I’m not sure what you mean by that.

Do you want me to put the AngleBetweenObjects in the instance variable, or elsewhere?

Also, when I try to insert the mouse X and Y positions, it doesn’t work. It only allows one or the other.

Would something like this work if I convert the instance variable back to a string?

Either as a variable, or directly to guy.angle. Something like :

No, it won’t work. GDevelop does not evaluate a string. If you put a function, like MouseX(), as the value for variable when you define it like this :

it gets treated as a literal string. GDevelop will consider it as “MouseX()”, an 8 character string. It will not set the variable to the value of whatever MouseX() is, it will set it to the literal string.

To set a variable to the value of a function, you need to set it in an event :

This evaluates MouseX(), and puts the resulting value into aimTarget.

1 Like

I changed the code and got some different results.
The angle now changes, but it only does so at the beginning of the scene, then locks the bullets into shooting in that direction, instead of always shooting towards the mouse.

Also, the angles are really random. It shoots in whatever direction it feels like, depending on the position of the mouse.

Is there a remedy to this?

What does aimTarget do? And you’re also referencing OGangle in a previous screen snip. Is this interfering with the aiming?

For the firing, you should really be getting and calculating the angle just before you create the bullets. You want a fan, with the middle aimed where the mouse is positioned. So the starting angle should be something like

`AngleBetweenPoints(guy.X(), guy.Y(), MouseX(), MouseY()) - (((guy.Variable(bulletAmount) / 2) * guy.Variable(fanSpread))`

and then add fanSpread to what ever variable held the start angle.

1 Like

To rephrase what MrMen mentioned above, since someone else just asked me something similar:

You cannot put expressions (of any type) into a precreated variable field. Expressions only work in events. You are welcome to precreate the variable name but you will have to leave a default value and then set the expression via events.

2 Likes

Alright, I modified the code. I had to change it up a bit to get it to work

The disabled code below works great, the fan is now firing directly towards the point I want it to, no matter the bullet amount or spread.

However, the active code (The one that tracks the mouse) nets this result:

Here, the bullets are all bunched up together instead of spread out, and they’re not aiming directly for the mouse, but they’re aiming for an angle above the mouse instead.

I feel like I’ve almost got it. Is there a way to make the aim accurate and spread out the bullets again?

You haven’t referenced guy.Variable(angle) inside the repeat loop, so the bullet angle isn’t changing.

Yes, that’s the start of the fan firing, the row of bullets that are most anti-clockwise.

Yep, by changing it to this :

1 Like

Sorry for the late reply, but it worked! The fan now aims towards the mouse!

I’ve went ahead and made toggleable options to aim for the mouse, aim at a set angle, and at a specific target.

The target code needs some fine tuning though.

I have this test object moving back and forth with a sine behavior, and I’ve set the guy to aim at it:

However, it’s because I have the code set up to do this:

The instance variable “target” is a string, and my plan is to put in the name of the target, then the guy would shoot at the object with that name.

I was gonna use an expression like this:

`AngleBetweenPositions(guy.X(), guy.Y(), (guy.Variable(target)).X(), (guy.Variable(target)).Y())-(((guy.Variable(bulletAmount) / 2) * guy.Variable(fanSpread)))+(guy.Variable(fanSpread)/2)`

But the code runs an error when I type it. So is there a way to set a specific target for the guy to shoot at without hard coding everything? Or is hard coding the only solution?

Likewise, how do I get the guy to only shoot at the closest one rather than the first instance of it?

No, you can’t do that. For one, Variable returns a number. And secondly, even if you used VariableString, GDevelop doesn’t interpret the name at run time. It’s just treated as a string/text, and not as the name of an object.

I think you could achieve what you want by putting all the target objects into a group, and then use the string compare function on the group object name, and get the closest one (though not 100% sure as it’s not something I’ve tried before). Something like :

and then use TargetGroup instead of the object name in the subevents.

1 Like

Thanks a lot, mate, it works great now!