- Home /
Delay the execution of something
I'm new to unity and c# and want to know how i could add a delay for 4.5 seconds for this: void Update () { if (Input.GetKeyDown(KeyCode.S)) { anim.Play ("CharTurn"); (Delay here) transform.Rotate (Vector3.forward Time.deltaTime 10870) } The animation is a simple animation of the character turning around, and it last for 5 seconds if you could keep it as short as possible that would be much appreciated
Answer by HawkSandwich · Jun 19, 2017 at 06:08 AM
Heya! This is a simple thing to solve, but the answer might make you a little unhappy. In JavaScript, it's as simple as adding one line to make it wait, in any function. In C#, however, you can't wait unless you're in what's called an IEnumerator. So to do your waiting, you'll have to make another function and call it.
void Update () {
if (Input.GetKeyDown(KeyCode.S)) {
StartCoroutine(ThingyThing());
}
}
IEnumerator ThingyThing () {
//do the animation thing
yield return new WaitForSeconds(5f);
//do the rotation thing
}
Hope I helped :)
@HawkSandwich thanks, I've decided to go against this originally and try something else,but if I change my $$anonymous$$d again (which I really might do, I'm very indecisive) this will really help, and is defiently going to help with delaying things in the future
No problem, glad I could help :) $$anonymous$$aybe consider marking my answer as "Best Answer"?
Your answer
Follow this Question
Related Questions
How do I put a delay in this? 2 Answers
Idle animation plays while running (2D) 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Shot Delay between button press. c# 2 Answers