getkey, keyup, keydown
Posted earlier trying to fudge a stamina bar as everything I tried online is just too far off what I have to work ( and " yes" I tried VERY hard to adapt). SO my fudge is that since the left shift is the sprint, I have a script that brings down my stamina bar. I had to use "GetKey" as opposed to "GetKeyDown" because GetKeyDown only moved the bar one increment where GetKey steadily moves the bar as long as it is pressed. My problem is that I can't figure out how to reset the stamina bar. I was hoping to use "GetKeyUp" with the same key so when I release the left shift and stop running the bar would slowly replenish. But as you can see there is a conflict as I can only do the GetKeyUp once and it only replenishes one increment ( same issue I had with depleting the stamina bar). How can I reset the Stamina bar so in 5 seconds after I do "GetKeyUp" the bar will be full again? I know I could just set the amount to 100 and fill it up again but there needs to be some down time after a sprint This is the code I have for the bar
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class newStaminaTry: MonoBehaviour
{
public float startingStamina = 100;
public float currentStamina;
public Slider StaminaSlider;
void Awake ()
{
currentStamina = startingStamina;
}
void Update ()
{
if (Input.GetKey(KeyCode.LeftShift))
{
currentStamina -= .5f;
StaminaSlider.value = currentStamina;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
currentStamina += 10f;
StaminaSlider.value = currentStamina;
}
}
Answer by shadowpuppet · Aug 13, 2016 at 07:21 PM
Glad to somebody is answering my desperate cries for help. Saw you responded to my nearly identical question so i thought I'd responb to this one as well. Short version I made GetKeyUp set a bool and as long as that bool was true the meter climbed again . bool = false when meter hit 100
Answer by Landern · Aug 08, 2016 at 08:12 PM
track when you're draining as a constraint on regaining stamina, it's a concept, implement anyway you wish:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class newStaminaTry: MonoBehaviour
{
public float startingStamina = 100;
public float currentStamina;
public Slider StaminaSlider;
private bool drainStam = false;
void Awake ()
{
currentStamina = startingStamina;
}
void Update ()
{
if (Input.GetKey(KeyCode.LeftShift))
{
drainStam = true;
currentStamina -= .5f;
StaminaSlider.value = currentStamina;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
drainStam = false;
}
if (!drainStam && currentStamina < 100.0f)
{
currentStamina += 10f;
StaminaSlider.value = currentStamina;
}
}
}
Your answer
Follow this Question
Related Questions
lerp a float value of keyup 2 Answers
How do I take out a UI Slider's value? 0 Answers
How to control animation with slider 4 Answers
Slider with filter / search engine -1 Answers
Slider value help 0 Answers