Download

Charge Weapon

Hi, I would like to know if anyone can tell me how to setup a blueprint to. Hold down a key to charge a weapon. And when you let go of the key it shoots.I want to be able to charge up a particle system like a fireball.

On Click -> bool bIsCharging -> float charge += chargeRate
-> if charge == maxCharge -> shoot -> bIsCharging = false
-> else if ButtonRelease -> shoot -> bIsCharging = false

That’s the logic, same applies to blueprints. Obviously remember to reset the charge if the character dies, see if he’s holding a weapon to charge, etc etc.

Sorry im still new at all of this but i tried the set it up how you explained it is this what you ment?

bfeeb8ad59a0324627abe734cd4a409d0459fdf2.png

The problem with that setup is that you aren’t actually incrementing your charge value… and you aren’t passing that charge value onward to the projectile!

Try something more like this.

89acd57997368cb152bdcfa4bd27a47eb8b6e24b.png

Note: I use an Int value incremented by one at a fixed rate of time… so I have a short timeline which loops (at 0.1 in this case) and fires a single event every time it loops. If you want your charge to be more fluid, rather than working in discrete states, you’d instead want to set your Timeline up to have a length equal to the total charge time, and then add a float track that goes from 0 to 100 (or whatever max charge is) over that time. You would just have the Timeline’s main output pin set the charge state directly with that output float, rather than doing the incrementing math that I do, and then its “finish” pin would fire the shot if you wanted auto-fire.

Two schools of thought on the subject, I suppose.

Ok Thanks this works. But there are a few things i do not understand. Like the part about pluging the int into the spawn projectile there is no way it can be plugged in there. Also how can i stop it from firing without charging first.

Does anyone know how to fix this?

Okay, so for the first bit:

On your projectile BP, create a variable (Int in my example case) and click the Eye icon next to it. Also, in its details pane, check “expose on spawn”. What this does is tells Unreal “this variable is not just used by the BP internally, but I should be able to set it when I create an instance of this actor”. That will make that variable appear as a pin when you select that BP in the Spawn Actor drop down (you may need to delete and recreate the node after compiling the projectile BP).

BUUUUUUUUT, that only matters if the projectile is chargeable! i.e. it changes (does more damage, gets bigger, whatever) the longer you charge it. It sounds like you want more of a windup-type attack, where you have to hold the button down for a certain amount of time to prep the shot and releasing early causes the attack to fail. You would do that completely differently.

To do that, you would make your Timeline have the length of the warmup/chargeup, and connect its “finish” pin to the projectile spawner. You would tell the Input Action pressed to “Play from Start” and the released to “Stop”. So every time you press the button, it plays a countdown (from the beginning) and then fires the projectile at the end of that countdown, but if you release it before it gets there, it stops counting down.

Or if you mean “it’s chargeable, but it won’t do anything unless it reaches some minimum charge point”…? Like the tiers are “do nothing, little attack, big attack, huge attack”? In that case, right before the projectile spawner you’d have a branch that checked if the Charge State variable was above a certain amount. If true, it would fire the projectile and reset the charge state… If false, it would just reset the charge state without firing.

Thanks that fixed it!. But there is one thing happening now that is kind of weird. When i hold down the charge button if i hold it down past a certain time it autofires and then when i let go of the button it fires again.Is this something to do with my timeline or what?

Oh, that’s my fault, sorry, I honestly forgot about that scenario.

Create a Bool variable called “AutoFired” and set it to true when an auto-fire happens.

From your release pin, before you reach the projectile spawner, put a branch, with the condition being “Auto-Fired”. If it’s false, spawn the projectile; if it’s true, skip spawning it and go right to resetting your Charge State. After your charge state resets, add a Set AutoFired to false.

What this does is handles that case; if you auto-fire, releasing doesn’t do anything. You must release and then press again to fire a second time.

Ok that fixed the double fire problem. But now it only fires once and then it will not happen again. Ill show how i have it setup.

Are you sure you’re properly resetting “AutoFired” to false EVERY time you release the key? If you missed a circumstance (i.e. a branch which has a false pin that does nothing rather than going to that reset logic) this will break.

Im sorry im not sure what you mean. I new to booleans and blueprint.

Okay, I’m looking at your image and… if your fireball can ONLY be fired by fully charging it, why bother with the charge state at all? You could remove the entire “charge” execution track from the timeline, delete your charge state variable, remove the “auto fire” var, and simply tell it to fire when the timeline is done.

I’m sorry, man, I’m not entirely sure what you’re after with this. Can you describe to me exactly how you want this fireball to function in game? i.e. how do you want it to behave if you hold down the fire button and release it? If you hold it down without releasing it? If you tap it? If you hold it down for a short period and release it? What, specifically, do you want this subsystem to do?

There are a lot of different ways that a charging fireball attack could work, and I can’t figure out what’s wrong with your BP until I can figure out what exactly you’re TRYING to make it do.

Like This.

https://youtube.com/watch?v=8I5ifBB_XXc

Its basically the same as in the video. Hold down a key until you want it to be fire. The longer you hold it down the better it is. right click to cancel.

And you want it to auto-fire at max charge, right? Okay, so this is what you need to do:

48d41b7eb7faea9fe3d73a3485982890730b8fbe.png

See if you can figure out WHY this works and why each part is necessary, it will actually teach you a lot about BPs because this system you’re creating requires a lot of tricks to do!

If it helps, you might rename “auto-fire” to something like “break release”… that’s what it actually does. It creates a flag and when it’s set, releasing doesn’t shoot the fireball. The two circumstances here where it’s used are to break release on an auto-fire (so releasing doesn’t fire if the fireball already fired itself) and to break release if you cancel charging with the cancel charge button. But there are other situations where you might want to use it; for example, maybe if the player is hit, or maybe if he falls into water, or gets hit by an ice spell, or whatever, you want to nullify his fire spell. You might use “break release” as a part of that subsystem to prevent him from firing off a fire spell he was charging when that change took place (you might also create a custom event which goes to the same execution path as the “cancel” command, and simply call it whenever you wanted to cancel his fireballs. That might be easier).

Note that if you’re going to do something where charging the attack causes it to consume more magic points, you’ll need to do some other stuff. For example, you’ll have to connect the max charge value to a “min” node which has the max charge value as one input, and (the MP var) / (MP used per charge state)] for the other, so you can’t charge it beyond what your current MP would allow. You’ll also need to call up the logic which decrements the player’s remaining MP based on the charge state BEFORE you clear it back to zero after firing, or you’ll run into a bug where the spell doesn’t consume MP because you’ve zeroed out the charge immediately after spawning the fireball.

Ok Thanks. I messed with what you posted a bit and got it to work how i wanted. Now i was working on changing the where and how it spawns and i was wondering if you could help. i have it setup so it will spawn infront of me and go to the location im looking. But for some reason it snaps to the ground and moves along it.(i.e if i look up and shoot the fireball it spawns infront of me but on the ground and moves from there). Can you tell me what i did wrong?. Sorry if im being a bother.

Snaps.png

Does anyone know how i can fix it so it shoots where ever im looking and does not snap to the ground.

I still have not been able to figure this out :confused:

I thought you were spawning a projectile? What’s the trace for? In your screenshot, I can’t even see you doing anything with the hit struct output…?

The trace was to spawn it where im facing