- Home /
Question by
rdake45 · Oct 18, 2019 at 01:31 AM ·
update problempower-up
Is there a way to use yield return new WaitForSeconds(5f); in void update or do i need to do somethign else?
I am trying to use yield return new WaitForSeconds(5f); in void update and i can't find out how to use it here is the script.
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
gameObject.GetComponent<FirstPerson>().moveSpeed = (10);
yield return new WaitForSeconds(5f);
gameObject.GetComponent<FirstPerson>().moveSpeed = (5);
}
}
}
Comment
Best Answer
Answer by cdr9042 · Oct 18, 2019 at 02:22 AM
You can just call StartCoroutine
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
StartCoroutine(Slowdown());
}
}
IEnumerator Slowdown()
{
gameObject.GetComponent<FirstPerson>().moveSpeed = (10);
yield return new WaitForSeconds(5f);
gameObject.GetComponent<FirstPerson>().moveSpeed = (5);
}
what happens if player press E again when the previous Coroutine hasn't finished yet? you might wanna add more check in there, otherwise multiple Coroutines will run simultaneously when the previous hasn't finished yet
Thanks! The script worked lovely and thanks for re$$anonymous$$ding me about the cooldown.