I’m currently using Unreal Engine 4.6, but I’m having a few problems with blueprints. I’ve set up so that if you press left shift and move character the character will sprint. I want to implement where if you sprint it will deplete your stamina progress bar, but because the stamina bar is empty you won’t be able to sprint in till it recharges. That’s the issue I’m having…the character is still able to sprint even with an empty stamina bar, and I would like to prevent the character from sprinting in till the stamina bar has recharged. Here’s a picture of the blueprints, and if its not too much trouble could you please help with this issue.
Kind regards,
Oh my
So first of all you always hardcode a value to your stamina. Not too bad if it’s intended but for example I’m pretty sure once you press sprint you shouldn’t automatically gain 0.5 stamina. This would allow to always sprint anyway if you press the button super fast!
Secondly: All your branches aren’t doing anything right now. They have no condition. They are just always ture so everything will always be executed (which includes setting the max walk speed to 600).
To completely fix this I should probably show some pictures but I’m not in reach of an engine right now.
I hope this already helps. I’ll add some pictures later today hopefully to show what exactly I mean and to show how to get this working.
Cheers
I’ll really appreciate that. p.s I also want to implement a spell mechanic, so when you press ‘C’ it’ll cast something like a fireball. I’ll be implementing a magika progress bar, so when your magika is low then you won’t be able to cast fireballs anymore. I know how to implement progression bars because I’ve done it previous for stamina and health, but that problem is that I don’t understand the process of preventing the player from casting fireballs when magicka is low and also the blue print scripting for casting fireballs. Do you have any knowledge on how I would accomplish this?
I don’t understand how to prevent the person from sprinting either.
Update: Created a projectile blueprint, which casts a fireball when you press the ‘C’ key. However, I am now on the stage of implementing this with a progression bar. I just have no idea on how to prevent the player for casting fireballs.
Alright here we go:
First of all I highly recommend using action and axis mappings rather than hard coded keys (likey our left shift). This will allow you to rebind keys later on.
You do this by clicking on project settings
and then under input click the plus next to action mappings, name it “Spring” and select “Left Shift” from the drop down menu like this:
Then you have a node called “InputAction Sprint” (you can find it in the context menu by searching simply for “Sprint”). It has exactly the same outputs.
Now for the code itself. My suggestions looks like this:
To how it works: I tried commenting it as well as possible but it does need a bit of explanation.
“Sprint Pressed” goes into a branch. It checks if you have more than 1 stamina. Basically just making sure you can spring for a relevant time (1 second in this case).
If that is true the movement speed is set to 600 and it opens a gate. A gate is pretty much exactly what it’s called after. You have one exec input (white line) into an enter field. If it’s open the input will be allowed through otherwise it will be blocked.
The gate “Enter” is connected to an “Event Tick” which will be called each frame (so with 60 fps you’ll have it called 60 times a second).
I then also use the other output (“Delta Time”) and multiply it by 1 (pointless in this case but now you can change the cost per second ;). Delta Time gives you the time it took in seconds. So something around 0.01 and a bit. This will result in the correct amount of reduction of stamina and stop it in exactly the correct frame.
If stamina - this new value is below or equal to 0 the stamina is set to 0, the movement speed is set to 300 and the gate is closed (no stamina will be reduced anymore) otherwise it will simply reduce the stamina by the value we got.
To regenerate stamina you can do a little something like this:
Basically I’m adding a boolean to the code so we can always check anywhere if we’re running right now or not.
If we are running it will reduce the stamina as we’re used to but if we are not running it will regenerate stamina by our set amount (1 again in this case).
I had to split it up into two comments because of the 2k character limit
I hope this helps you. If you have any question about anything I did don’t hesitate to ask!
Cheers!
I’ll attempt your method right now. Thank you so much!
The stamina bar decreases, and then increases which is the thing that I wanted, but for some reason I can’t sprint…I was wondering if you could check the script and tell me what I’ve done wrong. Are there any learning sources to learn blueprints?
Update: Never Mind, I fixed the issue. I didn’t connect the branch to the multiply float. This is the what I wanted. Thank you so much, you don’t understand how grateful I am. Would this be the same progress for when I want to prevent the player from casting spells when magika is low?
I have a new issue now. I had the stamina decrease when you jumped by 0.25, but then it increases straight after. However, since I’ve followed your method the 0.25 isn’t decreasing anymore. How would I fix this if you don’t mind helping me out again?
I’ve decided to leave the jumping function at the moment so that I can work on implementing magic into my project. However, I tried to take the same approach to the stamina, but the progress bar isn’t even decreasing at all. I have no idea if I’m doing it correctly, but I was wondering if you could help me out with this issue? I feel like I’m asking for too much help, but any help will be appreciated. Here’s a picture of the scripting so far:
Sorry for being so late but your questions were around 2 am in the morning for me
Alright so for the jumping you actually only have to take away the stamina gain on stop jumping. I mean just because you don’t fly higher you didn’t catch your breath did you? ;D
For the magic system… Maybe you want to implement a system where spells cost magicka and not the casting duration. This would make everything but the regeneration obsolete but you will have to spawn a projectile or do something which costs mana on button press.
I’m again at University but I’ll once more give some pictures later.
Oh yes and check out the unreal engine YouTube channel. They have a lot of awesome tutorials!
Alright so for your jumping question.
Somewhere in your character or controller you have a key binding for jumping. You will have to check there if your stamina is below 0 or 1 or 0.25 or whatever you wanna check against as well.
To reduce magicka is fairly straight forward. Spawn your spell and then reduce your magicka value by the amount you want (maybe store the cost in your projectile so you can get the cost dynamically from each projectile and don’t have a static cost for all projectiles but that’s even deeper in… know what? I will make a video about interfaces and variables this evening. This is actually asked quite often and it’s probably handy to have anyway :D.
But back to the question. It’s exactly what you did earlier with your stop jump. Just reduce the variable by X
Yes, I would like to have it so each individual spell cost magicka not the casting duration. I’ve already made a projectile already, which I’ll include an image below. ^^ If you could show me some pictures of how to implement a system where spells cost magicka that would be much appreciated. From that foundation, I guess I can keep adding projectile afterwards? ^^ If you don’t understand how grateful I am for your help. Thank you!
Sorry typo error…I meant to say, ‘you don’t understand how grateful I am for your help. Thank you!’
The stamina depletes when you press the space bar, but is there a way to prevent the player from jumping when the stamina bar is at 0? If you spam the space bar while in mid-air as well, the stamina bar still decreases. Is there a way to prevent that as well? The magic system is the main aspect at the moment though, because I have literally no idea on how to start that.
A video would be so helpful. I’m guessing you’ll link the video when it’s made? I was planning to make a projectile for each spell, so basically I could alter the magicka cost for each projectile to make it dynamic? However, that’s more confusing as well. How would I be able to toggle between different projectiles?
I will add that in the video as well
Though that goes pretty much into the inventory type of thing and I won’t be able to add a full UMG explanation as well.
Basically you have your main slots which can be used by keys and an inventory full of all possible spells.
Then you have to be able to set spells from your inventory to your active slots maybe with unlocking as well and all that good stuff.
I will just explain how to get the different projectiles from those main slots.
An inventory of this style would certainly deserve it’s own question and quite a bit of research on a few different tutorials from your side
I’ve already made an inventory using UMG. I’ll attach a few images to show you the scripting and designer tab ect…I’m hoping that this is relevant towards picking different spells, because then I can just simply use the current inventory scripting as a guidance. I followed a very helpful tutorial that I found on YouTube, but that tutorial only went into the basics on UMG.