Decreasing Slider value if a button is pressed
I'm new to unity and I have a problem with the slider option in unity. What I have been trying to do is that if the left shift key is held down the staminaslider.value will decrease and when its released it goes back up. But I haven't figured a way to fix this.
The code I'm using:
if (Input.GetKeyDown(KeyCode.LeftShift) && (currentStamina > 0))
{
currentStamina -= Time.deltaTime;
speed = (7.0F);
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = (5.0F);
currentStamina += Time.deltaTime;
}
StaminaSlider.value = currentStamina;
Answer by Voyder_Rozann · Feb 24, 2017 at 11:20 AM
@Fullmetal16 Are you using 'void Update()' ? Because, if you use this in 'void Start()' or 'void Awake' it will not work at all ! But I can't see all of your code so I make my opinion for the Update function !
Answer by Fullmetal16 · Feb 24, 2017 at 01:54 PM
@Voyder_Rozann yes all of this is in the void update ()
Ok so add a new float called, for example, 'ActualSta$$anonymous$$a' ! Then multiply this value to the Time.deltaTime !
Here's an example :
public float ActualSta$$anonymous$$a;
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift) && (currentSta$$anonymous$$a > 0))
{
currentSta$$anonymous$$a -= ActualSta$$anonymous$$a * Time.deltaTime;
speed = (7.0F);
}
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftShift))
{
speed = (5.0F);
currentSta$$anonymous$$a += ActualSta$$anonymous$$a * Time.deltaTime;
}
Sta$$anonymous$$aSlider.value = currentSta$$anonymous$$a;
}
If it does'nt work... I can't help you further ! :/