Тёмный

How to Make an Automatic Recharging Stamina Bar [Unity Tutorial] 

Comp-3 Interactive
Подписаться 16 тыс.
Просмотров 35 тыс.
50% 1

In this video we'll create a super simple and super easy to use stamina bar for your RPG games. This bar will deplete when an action is performed and then start to regenerate / recharge after a set amount of time!
Join me and learn your way through the Unity Game Engine, the C# language and the Visual Studio editor. Remember, if this video was useful then DROP A LIKE! 👍
💬 Join the Discord community here:
/ discord
📱 Find us on social media for more Tips and Tricks:
/ comp3interactive
/ comp3interactive
📱 Play our games for FREE here on Google Play:
bit.ly/2TisAQo
☕ Did I help you out? Feeling generous? Consider buying me a coffee/beer🍺😍: www.buymeacoff...
-- CREDITS --
Intro music courtesy of www.bensound.com/ #UnityTutorial #Unity3D #Comp3interactive #GameDev

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 139   
@jarrydoneill7834
@jarrydoneill7834 2 года назад
This video was a true lifesaver. I'm currently creating an RPG and I was trying to get a running system in place and I was battling for few days until I came across this video and it helped me figure it out. Thank you!
@jeffreyjumisko5165
@jeffreyjumisko5165 2 года назад
For a short tutorial, you sure packed in some functionality. Thank you!
@brettsilverman9770
@brettsilverman9770 2 года назад
Great video! I actually used this code to make my health regen and it worked so smoothly! Thank you so much for the tutorial! I’ve been looking for a good one for hours.
@erichalvorsen7645
@erichalvorsen7645 3 года назад
THis video was easy to understand, concise, and time saving. Thank you very much!
@marigold8374
@marigold8374 2 года назад
An easy to follow yet effective tutorial, thanks!
@TopThreePlayz
@TopThreePlayz 3 года назад
Thank you so much this worked for my health bar as well and my shield bar! :D
@randomperson.0-0
@randomperson.0-0 2 года назад
hi,i've been wondering how do i disable a specific key when the currentStamina drops to 0 and it seems like you know how to code so if you could help me i would appreciate it
@avisharsharan9693
@avisharsharan9693 3 года назад
Very helpful! Great explanation!
@mageproject7002
@mageproject7002 3 года назад
Very nice! This is what I needed for my game! Thanks.
@tech-boy334
@tech-boy334 2 года назад
How do i get it to degenerate not in ticks but in a smoother way like the re-gen?
@rajatkoparde5224
@rajatkoparde5224 2 года назад
hey man, great tutorial. I have a simple question..... how can I make stamina to slowly decrease on holding down some key( like space) then after releasing it should regenerate slowly
@xlunyl
@xlunyl 2 года назад
You can do a a few methods but here is the full question answers.unity.com/questions/1220440/how-to-display-call-a-function-every-second.html
@BlakeJPhoenix
@BlakeJPhoenix 4 года назад
Great tutorial! It was very helpful
@supertenchoo4271
@supertenchoo4271 2 года назад
This is a great tutorial with clear explanation thanks man
@rafarodriguez4765
@rafarodriguez4765 3 года назад
Advanced level tutorial. Keep doing so well. Thanks.
@Moomit
@Moomit 2 года назад
Awesome video! Had just what I needed. Thank you!
@cugames1676
@cugames1676 2 года назад
Great tutorial, needed something like this to make my game a bit more interesting, thanks!
@ashwath8400
@ashwath8400 2 года назад
Very useful man actually I wanted to create a auto reloading ammo bar (over time). figured out bcoz of you thanks 👍
@migueltrinidad6244
@migueltrinidad6244 Год назад
amazing job! thanks for the super useful tutorial buddy
@Праведныймиротворец
This is such an effective yet simple and concise code! Thank you
@SnakeOfficial
@SnakeOfficial 3 года назад
Ill keep getting The type or namespace name 'stamina' could not be found (are you missing a using directive or an assembly reference?) you had same problem?
@Праведныймиротворец
@@SnakeOfficial I did not get this error. Are you certain in the script (in the inspector tab) you are pointing towards the slider UI prefab?
@SnakeOfficial
@SnakeOfficial 3 года назад
@@Праведныймиротворец how do you mean that?
@Праведныймиротворец
@@SnakeOfficial Check out 6:20
@Праведныймиротворец
@@SnakeOfficial He drags the script into the slider object. Then in the inspector tab (on the right), the script appears at the bottom. He then drags the slider object into the script reference.
@iluz1eiluzie370
@iluz1eiluzie370 3 года назад
Great tutorial thanks really helped me
@herbou
@herbou 4 года назад
Great tutos 👍
@monkeyrobotsinc.9875
@monkeyrobotsinc.9875 Год назад
Damn good
@ilet2420
@ilet2420 4 года назад
Thanks man!
@BattlePlayer11
@BattlePlayer11 2 года назад
Works perfect, Thank you!!
@just_danah
@just_danah 3 года назад
Thanks for the Great Tutorial! Really helpful got the stamina bar working! I was wondering though how you would make it so the player cant sprint anymore once the bar is empty ive been struggling and im at a lose as to how to code this
@LION303200
@LION303200 4 года назад
Amazing thank you very much, that was awesome, i was looking for this for a whole week.. you are the only one use int not float , thanks
@rudragoenka4572
@rudragoenka4572 4 года назад
thanks dude you are a life saver
@Estelelenath
@Estelelenath 3 года назад
perfect lecture thank you! but one question!, until 10:00 i did follow perfectly, but from "private Coroutine regen;" i did same as you, but stamina is not regenerating, could you know, why it is?
@williamhiciano394
@williamhiciano394 3 года назад
same happened to me, but i set stamina to a value of 50, when i put it to a value of "100" it worked
@reapercolt4900
@reapercolt4900 3 года назад
same. did you find out how to do it???
@williamhiciano394
@williamhiciano394 3 года назад
awesome bro
@Zygmuntt
@Zygmuntt 2 года назад
Thank you!
@dennisgonzales9521
@dennisgonzales9521 4 года назад
Awesome!
@chrismakescrappyvideos4715
@chrismakescrappyvideos4715 4 года назад
For all you babies in the comments, here's the script. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Collections; public class StaminaBar : MonoBehaviour { public Slider staminaBar; private int maxStamina = 100; private int currentStamina; private WaitForSeconds regenTick = new WaitForSeconds(0.1f); private Coroutine regen; public static StaminaBar instance; private void Awake() { instance = this; } // Start is called before the first frame update void Start() { currentStamina = maxStamina; staminaBar.maxValue = maxStamina; staminaBar.value = maxStamina; } public void UseStamina(int amount) { if (currentStamina - amount >= 0) { currentStamina -= amount; staminaBar.value = currentStamina; if (regen != null) { StopCoroutine(regen); } regen = StartCoroutine(RegenStamina()); } else { Debug.Log("Not Enough Stamina"); } } private IEnumerator RegenStamina() { yield return new WaitForSeconds(4); while(currentStamina < maxStamina) { currentStamina += maxStamina / 100; staminaBar.value = currentStamina; yield return regenTick; } } }
@gravityfallsisawesome9029
@gravityfallsisawesome9029 4 года назад
It works for the most part but I want it to go down while the player is running not when you click the button
@TheAntonio322
@TheAntonio322 Год назад
you are a god damn god!
@darrellahmedgalen9344
@darrellahmedgalen9344 3 года назад
Finnaly found it!!,Thanks for this tutorial>//
@craftalesinc8131
@craftalesinc8131 2 года назад
TYSM :)))
@behnamrax
@behnamrax 4 года назад
Thank you very much
@hungrygrimalkin5610
@hungrygrimalkin5610 4 года назад
I tried to make this into a spaceships regenerating shields but it won't start regenerating, I put the coroutine into update but still nothing, any help?
@zeus7477
@zeus7477 3 года назад
Awesome
@ir3dyplayz78
@ir3dyplayz78 3 года назад
I have a question how do i refrence my stamina bar is full or is empty to either start or stop running? this is what i have on my player controller for sprinting.. void Sprint() { if (Input.GetKey(KeyCode.B)) { StaminaBar.instance.UseStamina(0.5f); walkSpeed = runSpeed; } else { if (Input.GetKeyUp(KeyCode.B)) { walkSpeed = 3f; } } } im using floats instead of int just so i can use decimals...
@jorgevelasco-theartofgames8687
@jorgevelasco-theartofgames8687 4 года назад
Great tutorial. I dont understand the diference between "UnityEngine.UI" and "UnityEngine.UIElements"
@jonordona9733
@jonordona9733 4 года назад
Thanks for this, truly helpful. I've been experimenting and realized that tweaking the maxST/200 and regenTick you can acheive a much smoother regeneration. How costly do you think it is to change the tick from 0.1f to 0.01f?
@comp3interactive
@comp3interactive 4 года назад
I wouldn't have thought it would be too much of an impact. You can always runs a test using your Profiler and see if there's any major problems
@tech-boy334
@tech-boy334 2 года назад
How do i get it to degenerate not in ticks but in a smoother way like the re-gen?
@themaskyyt
@themaskyyt 3 года назад
Hi, may I ask why did you decide to use integers instead of floats for the values?
@comp3interactive
@comp3interactive 3 года назад
My stamina values would always be whole numbers, floats would work too but just to remove the need to round off the floats I chose ints
@LunauticalDev
@LunauticalDev 4 года назад
Thanks! I can say that this still works in Unity v2020.1.0f1. Though, I want this to be so that the player could only run if the stamina isn't = to 0. But I'm doing this in another script. So how would I grab the currentStamina from this script to another?
@Arnmazing23
@Arnmazing23 4 года назад
Sir Lunatic Staminabar.instance.currentStamina
@joaofonst
@joaofonst 4 года назад
@@Arnmazing23 But you have to turn currentStamina a public variable, or it wouldnt work.
@baaldorf
@baaldorf 4 года назад
Thanks
@colorfury6936
@colorfury6936 2 года назад
nvm i fixed it but great video!
@SnakeOfficial
@SnakeOfficial 3 года назад
I keep getting this error: I changed the name to StaminaBar, staminaBar, stamina, I dunno what to do The type or namespace name 'stamina' could not be found (are you missing a using directive or an assembly reference?) The error is on line 13: public static stamina instance;
@comp3interactive
@comp3interactive 3 года назад
That line needs to match what your class is called. So if your actual class is called StaminaBar then you need public static StaminaBar instance;
@SnakeOfficial
@SnakeOfficial 3 года назад
@@comp3interactive Yes, I did this, exactly like in your Video. I also tried some different things, renaming everything etc.
@comp3interactive
@comp3interactive 3 года назад
Sorry but you can't say you did it exactly as the video because in the video it works. Check your spelling and capitalisation of the words. It's saying it can't find a class or namespace named "stamina" which means it's spelled wrong or doesn't exist in your project
@SnakeOfficial
@SnakeOfficial 3 года назад
@@comp3interactive okay, I was able to fix it. Thx.
@unitedgamingalliance772
@unitedgamingalliance772 3 года назад
Anyone know how to disable functions when stamina is
@McMorgatron
@McMorgatron 3 года назад
hey mate, i tried something and it worked within you're player movement/action script: create a variable : public StaminaBar staminaBar; then before you're movement functions, use if(staminaBar.GetComponent.currentStamina >= 10) { ****your action function goes here etc**** } then when saved return to unity and drag your slider/staminabar gameobject into the new 'Stamina Bar' section of your player game object script
@unitedgamingalliance772
@unitedgamingalliance772 3 года назад
@@McMorgatron Thanks but I had already figured it out
@supertenchoo4271
@supertenchoo4271 2 года назад
How to make slider go smooth am trying to use Time deltaTime but it's not smoothing any things I want to make it move like fluid
@tech-boy334
@tech-boy334 2 года назад
How do i get it to degenerate not in ticks but in a smoother way like the re-gen?
@alexeykriuchenok5636
@alexeykriuchenok5636 Год назад
doesnt work for me((( stamina still regenerates if i run when it starts regenerating... so sad
@Chris-xe7xr
@Chris-xe7xr 3 года назад
Is there a way to change the slider to a image that drops down similar fashion?
@theangrypotato2.031
@theangrypotato2.031 2 года назад
im a little late, but you can actually adjust the direction a slider will drop in the slider, you can change it in "Direction"
@lucaswolynec1658
@lucaswolynec1658 4 года назад
7:05 just saving
@KAPPIEY
@KAPPIEY 4 года назад
*_Great video!_* *Question:* I was wondering why my regen is regenerating to full in one block instead of small increments like yours. (I didn't use your code exactly, but any ideas?). My code is working great other then that though! Really helpful.
@KAPPIEY
@KAPPIEY 4 года назад
I turned my whole numbers in the slider script off, wondering what else it could be...
@comp3interactive
@comp3interactive 4 года назад
Are you performing the regen as a co-routine?
@KAPPIEY
@KAPPIEY 4 года назад
@@comp3interactive Here we are, probably easier just to show you! lol using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Player : MonoBehaviour { public float speed; public int health = 10; public int stamina = 10; public Text healthDisplay; public int score = 0; public Text scoreDisplay; public GameObject dashEffect; public GameObject dashSound; private Rigidbody2D rb; // private Vector2 moveVelocity; public Slider healthSlider; public Slider staminaSlider; private Coroutine regen; //stop regening if dash is used again void Start(){ rb = GetComponent(); // Reference to rigid body in the start component } void Update(){ healthDisplay.text = "HEALTH :" + health; scoreDisplay.text = "SCORE :" + score; if(health = 2){ speed += 20; Instantiate(dashEffect, transform.position, Quaternion.identity); Instantiate(dashSound, transform.position, Quaternion.identity); staminaSlider.value -= 2; if(regen != null) StopCoroutine(regen); //stop regening if dash is used again regen = StartCoroutine(RegenStamina()); yield return new WaitForSeconds(.3f); speed -= 20; } else { Debug.Log("Not enough stamina"); } } private IEnumerator RegenStamina(){ yield return new WaitForSeconds(2); while(staminaSlider.value < staminaSlider.maxValue) { staminaSlider.value += staminaSlider.maxValue / 1000; staminaSlider.value = stamina; yield return new WaitForSeconds(0.1f); } regen = null; } }
@comp3interactive
@comp3interactive 4 года назад
I think it may be because you're starting a coroutine from inside or another coroutine. Try doing "yield return new StartCoroutine(RegenStamima())"
@KAPPIEY
@KAPPIEY 4 года назад
​@@comp3interactive *like this? it's giving me a syntax error saying:* *Assets/Scripts/Player.cs(80,23): error CS1002: ; expected* *Am i being stupid? I cant see any semi-colons that need adding?!* using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Player : MonoBehaviour { public float speed; public int health = 10; public int stamina = 10; public Text healthDisplay; public int score = 0; public Text scoreDisplay; public GameObject dashEffect; public GameObject dashSound; private Rigidbody2D rb; // private Vector2 moveVelocity; public Slider healthSlider; public Slider staminaSlider; private Coroutine regen; //stop regening if dash is used again void Start(){ rb = GetComponent(); // Reference to rigid body in the start component } void Update(){ healthDisplay.text = "HEALTH :" + health; scoreDisplay.text = "SCORE :" + score; if(health = 2){ speed += 20; Instantiate(dashEffect, transform.position, Quaternion.identity); Instantiate(dashSound, transform.position, Quaternion.identity); staminaSlider.value -= 2; if(regen != null) StopCoroutine(regen); //stop regening if dash is used again regen = yield return new StartCoroutine(RegenStamina()); yield return new WaitForSeconds(.3f); speed -= 20; } else { Debug.Log("Not enough stamina"); } } private IEnumerator RegenStamina(){ yield return new WaitForSeconds(2); while(staminaSlider.value < staminaSlider.maxValue) { staminaSlider.value += staminaSlider.maxValue / 1000; staminaSlider.value = stamina; yield return new WaitForSeconds(0.1f); } regen = null; } }
@atlantic6043
@atlantic6043 4 года назад
Big question so I am using your script with a few tweaks here and there and i just can't seem to find a good way to stop sprinting when the stamina goes out please reply as soon as possible
@comp3interactive
@comp3interactive 4 года назад
Just hold your state as a bool. IsSpriting, if you are sprinting and the bar runs out then toggle to false. Then deal with your speed however you need to deal with it
@teamnz3561
@teamnz3561 4 года назад
Awesome video really helped 🙏 but could you make an update video with an fps controller and when the bar goes all the way down the fpscontroller goes slower instead of still being able to run?
@the_goldendragon9158
@the_goldendragon9158 4 года назад
If you want that, then simply make it so that if whatever your stamina veriable gets to a certain value, you change speed. for example: If (stamina => 0) { speed = speed/2; } Hope this helped!
@rudragoenka4572
@rudragoenka4572 4 года назад
Please make a sprinting system which consists of this stamina script
@randomperson.0-0
@randomperson.0-0 2 года назад
how do i disable a specific key when the currentStamina drops to 0?
@theangrypotato2.031
@theangrypotato2.031 2 года назад
An if statement in your code could do the trick, for instance if(isGrounded == true && StaminaBar.instance.currentStamina != 0) { //Put your stuff here } This way if your stamina bar doesnt equal zero, it triggers the key, but if your stamina bar does equal zero, it can no longer run the code that has whatever you're trying to do, you can also always remove or add stuff inside of the if statement depending on what you already have there, or even just add one.
@commonbancho4364
@commonbancho4364 4 года назад
Boo, mine won’t regen...I don’t see why 😩
@amaterasu3364
@amaterasu3364 4 года назад
i am new and i was wondering if it is possible to make the stamina go down while holding the button ?
@thomashamilton1367
@thomashamilton1367 4 года назад
Just use GetKey instead of GetKeyDown and then decrease the stamina used so it doesn’t drain the bar immediately
@tech-boy334
@tech-boy334 2 года назад
​@@thomashamilton1367 Not quite sure what you mean by "Used" could you explain a bit more? You mean like" If (Input.GetKey(KeyCode. --button of you choice--)) StaminaBar.instance.Used();" Im not quite sure?
@ibhauptmann98
@ibhauptmann98 4 года назад
For me it says that "StaminaBar" before instance is incorrect.
@MotionMenace2
@MotionMenace2 4 года назад
any tips on how to implement this on a 2d character controller for a simple sidescroller? I have made a character that can attack, jump and dash, but i only want him to be able to if he have stamina enough according to this staminabar script. Any help is appreciated! Great simple video, i subbed! :)
@comp3interactive
@comp3interactive 4 года назад
To do this you'd need to check your stamina value is greater than 0 before performing the action. If it is then allow the player to perform it
@toxiconflict
@toxiconflict 4 года назад
@@comp3interactive how would that code look like though? I'm referencing the staminabar in my player movement script.
@LunauticalDev
@LunauticalDev 4 года назад
@@toxiconflict I'm finding this comment a month later. I just need to know how to reference the currentStamina part in a different script but idk how. Have you found a solution? (I believe we're looking to do the same thing lol)
@ir3dyplayz78
@ir3dyplayz78 3 года назад
@@LunauticalDev just call on the script... i.e. StaminaBar.instance.UseStamina(0.5f);
@ir3dyplayz78
@ir3dyplayz78 3 года назад
but if you wanna use it throughout the script use GetComponent();
@atapo7302
@atapo7302 3 года назад
When the bar starts to recharge... my game crashes. Any solutions?
@tech-boy334
@tech-boy334 2 года назад
you probably have a unclosed loop that is getting called
@jewis151
@jewis151 4 года назад
i forgot to put "using.UnityEngine.UI;" in and after i put it in it says "error CS100: Identifier expected" ive looked up everywhere but cant seem to fix it can someone pls help me
@comp3interactive
@comp3interactive 4 года назад
Remove the dot in between using and UnityEngine
@jewis151
@jewis151 4 года назад
@@comp3interactive thanks for the reply havent tried it yet i will see tomorrow
@jewis151
@jewis151 4 года назад
@@comp3interactive i feel immensely stupid right now
@comp3interactive
@comp3interactive 4 года назад
Don't, everyone makes mistakes man. Don't beat yourself up about it... Learn what was wrong and you won't make the same mistake again
@omegafala720
@omegafala720 11 месяцев назад
Bad, i want down key
@ethzalt84
@ethzalt84 3 года назад
10:42
@zedninja2773
@zedninja2773 4 года назад
This is not Roblox
@bruh-gy6yx
@bruh-gy6yx 3 года назад
It literally says "Unity Tutorial" in the title
@comp3interactive
@comp3interactive 3 года назад
I thought that when I read it... It's been 7 months and I still think about this comment and why he thought it was Roblox related 😂 it haunts me
@bruh-gy6yx
@bruh-gy6yx 3 года назад
@@comp3interactive 😂 Great Tutorial Dude! Im halfway through this video right now as of commenting this lol. Maybe someone linked him this video saying it was a roblox tutorial or something XD
@comp3interactive
@comp3interactive 3 года назад
Thanks man. How it helps you out! And maybe, I thought Roblox was just a game though
@bruh-gy6yx
@bruh-gy6yx 3 года назад
@@comp3interactive Well I guess you do have the Roblox Studio which uses LUA in scripting! Also, right now im trying to fix something but im not too sure how. Usually, with errors I can kinda understand what may be causing it and how to fix it but this one just confuses me XD so, In my script, I have a variable that looks like this: public static StaminaRecall instance; just like the instance variable you made but instead yours was called "StaminaBar" For some reason, when I put this variable in the script, I get this error: The type or namespace name 'StaminaRecall' could not be found (are you missing a using directive or an assembly reference?) This error would usually be caused when something inside a function dosent have a registered name. I usually get this error when I make spelling errors and I thought I did, but when I checked back, no spelling errors were present. I have tried multiple times to fix this but all my efforts result in more errors showing up. and Im not exactly sure how to explain this error, if I could, I would definetely look around google before coming to you. Im not sure how to fix this and any help would be appreciated! (Sorry for the whole essay of a comment btw! 😂)
@StrictlyStupid
@StrictlyStupid 4 года назад
TOO MUCH TYPING PLS JUST COPY AND PASTE IT I DONT LIKE TO TYPE THINGS WORD FOR WORD
@bruh-gy6yx
@bruh-gy6yx 3 года назад
copy and paste and you will never learn to code yourself!
@colorfury6936
@colorfury6936 2 года назад
as some who has a player script went into the thousands of lines ima tell you *your fine*
@Jordofentdealer
@Jordofentdealer 2 года назад
If you can't handle typing scripts yourself, I don't think game dev is for you. Really any coding related topic.
@StrictlyStupid
@StrictlyStupid 2 года назад
@@Jordofentdealer gee, thanks for making me give up on my dream bud, and just when i was about to do some finishing touches.
@roguewhitewolf
@roguewhitewolf 2 года назад
@@StrictlyStupid bro, don't guilt trip someone else for your stupid comments. grow up
@mixedtrigenito
@mixedtrigenito 2 года назад
heres the code without the instance and when you want to use it on your sprint or dodge input do public StaminaBar staminaBar; and drag the staminabar into on the editor and then call it "if (dodgeInput && staminaBar.currentStamina >= 25)" do something else do nothing; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class StaminaBar : MonoBehaviour { public Slider staminaBar; public int maxStamina = 100; public int currentStamina; private WaitForSeconds regenTick = new WaitForSeconds(0.1f); private Coroutine regen; // Start is called before the first frame update private void Start() { currentStamina = maxStamina; staminaBar.maxValue = maxStamina; staminaBar.value = maxStamina; } public void UseStamina(int amount) { if (currentStamina - amount >= 0) { currentStamina -= amount; staminaBar.value = currentStamina; if (regen != null) StopCoroutine(regen); regen = StartCoroutine(RegenStamina()); } else { Debug.Log("not enough stamina"); } } private IEnumerator RegenStamina() { yield return new WaitForSeconds(3); while (currentStamina< maxStamina) { currentStamina += maxStamina / 100; staminaBar.value = currentStamina; yield return regenTick; } regen = null; } }
@adslpiulentoditalia2545
@adslpiulentoditalia2545 2 года назад
I do not recommend using integer for this process. To have everything smoothly you need to use float. In my case I am using Image to get fill amount. This is my simplified script if anyone needs it. P.S instead of SecuredInt and SecuredFloat write float and int normally. public Image energyStamina; [SerializeField] private SecuredFloat maxStamina = 150; [SerializeField] private SecuredFloat currentStamina; [SerializeField] private SecuredInt numberToDivide; [SerializeField] private SecuredInt speedRegeneration; private WaitForSeconds regenTick = new WaitForSeconds(0.1f); private Coroutine regen; private void Start() { currentStamina = maxStamina; energyStamina.fillAmount = maxStamina; numberToDivide = (int)currentStamina; } private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { UseStamina(4); } } public void UseStamina(int amount) { if(currentStamina - amount >= 0) { currentStamina -= amount; energyStamina.fillAmount = currentStamina/numberToDivide; if (regen != null) StopCoroutine(regen); regen = StartCoroutine(RegenStamina()); } else { Debug.Log("not enough energy"); } } private IEnumerator RegenStamina() { yield return new WaitForSeconds(2); while(currentStamina < maxStamina) { currentStamina += maxStamina / speedRegeneration; energyStamina.fillAmount = currentStamina/numberToDivide; yield return regenTick; } regen = null; } for speedRegeneration i suggest you to set 1000 if you want it less slower but a good value is 500 / 1000 around
Далее
How to make a HEALTH BAR in Unity!
21:06
Просмотров 1,1 млн
ДОМИК ДЛЯ БЕРЕМЕННОЙ БЕЛКИ #cat
00:38
FIRST PERSON MOVEMENT in Unity - FPS Controller
23:53
Creating a STAMINA SYSTEM in Unity | Unity UI
20:32
Просмотров 30 тыс.
How to play random animations [Unity Tutorial]
7:36
Просмотров 21 тыс.
SAVE & LOAD SYSTEM in Unity
18:20
Просмотров 1,1 млн
PAUSE MENU in Unity
12:13
Просмотров 986 тыс.
UNITY BODYCAM!!! No need to switch engines?
4:55
Просмотров 3,1 тыс.
TOUCH CONTROLS in Unity!
16:09
Просмотров 1,3 млн