I’m making a POC which tests bullet patterns, and I’ve run into two problems.
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?
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 ^^
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(…).
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.
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.
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
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.
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?
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.