Right now I am working on a stamina bar for the player, where whenever he presses space the slider decreases, and whenever he lets go it increases. But the problem is I don’t want it to increase immediately after, instead wait 5 seconds for example. I tried using “yield new WaitForSeconds(5);” but that didn’t work as the statement is a void and has to be Coroutine. Does anyone have any idea how I can do this thanks!
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Stamina : MonoBehaviour
{
[Range(0, 25)] public float stamina = 25;
float maxStamina;
public Slider staminaBar;
public float dValue;
void Start()
{
maxStamina = stamina;
staminaBar.maxValue = maxStamina;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.LeftControl))
{
DecreaseEnergy();
}
else if (stamina != maxStamina)
{
IncreaseEnergy();
}
staminaBar.value = stamina;
}
private void DecreaseEnergy()
{
if(stamina>=0)
stamina -= dValue*Time.deltaTime;
}
private void IncreaseEnergy()
{
if (stamina<=25)
stamina += dValue*Time.deltaTime;
}
}