- Home /
Non Spammable Skill/Button
The delay only works at the start of the game,although I want it to repeat every time(after) i use the skill again, giving it a cooldown system, how do I achieve this, and it really got me confused :/ thanks for future replies, cheers! code:
{
public Text cooldownText;
public Canvas slaughterCanvas;
public Image imagecooldown;
public float skillcooldown;
public bool iscooldown;
public PlayerHealth scripthealth;
public GameObject weapon;
public float damageonSlaughterMode = 50;
// Start is called before the first frame update
void Start()
{
slaughterCanvas.GetComponent<Canvas>().enabled = false;
scripthealth = GameObject.Find("Player").GetComponent<PlayerHealth>();
}
// Update is called once per frame
void Update()
{
StartCoroutine((Wait()));
{
if (iscooldown)
{
imagecooldown.fillAmount += 1 / skillcooldown * Time.deltaTime;
if (imagecooldown.fillAmount >= 1)
{
imagecooldown.fillAmount = 0;
iscooldown = false;
slaughterCanvas.GetComponent<Canvas>().enabled = false;
weapon.GetComponent<Weapon>().attackdamage = 10;
}
}
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds (10);
yield return Input.GetKeyDown(KeyCode.E);
if (Input.GetKeyDown(KeyCode.E))
{
iscooldown = true;
slaughterCanvas.GetComponent<Canvas>().enabled = true;
weapon.GetComponent<Weapon>().attackdamage = 50;
scripthealth.playerHealth -= damageonSlaughterMode;
}
}
Comment
Best Answer
Answer by Orami · Feb 24, 2019 at 03:03 AM
use a timer...
public class someClass
{
public float cooldown = 10.0f;
private float timer = 0.0f;
public void Update()
{
timer += time.deltaTime;
if(timer >= cooldown)
{
Input.GetKeyDown(KeyCode.E)
{
//do stuff
timer = 0.0f;
}
}
}
}
EDIT:
Here is a small class you can use to make timers and an example of using it:
public class Timer
{
public float cooldown;
private float timer;
Timer()
{
timer = 0.0f;
}
public void update(float deltaTime)
{
timer += deltaTime;
}
public bool isReady()
{
if (timer >= cooldown)
{
timer = 0.0f;
return true;
}
else return false;
}
}
public class someClass : MonoBehaviour
{
Timer myTimer;
public void Start()
{
myTimer.cooldown = 20.0f; //set the cooldown to 20 secs
}
public void Update()
{
myTimer.update(Time.deltaTime);
if(Input.GetKeyDown(KeyCode.E) && myTimer.isReady())
{
//do stuff
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I delay an Ad? 2 Answers
Function delay? 1 Answer
Distribute terrain in zones 3 Answers
delay "after mouseclick" 0 Answers