Answer the question
In order to leave comments, you need to log in
Is it appropriate to use a coroutine here?
Hello.
Task: adding animation to the character when turning (pressing the A key - rotates by 90 *, pressing S - rotates by 180 *, pressing D - rotates by -90 *). After the turn animation has played (and the character turned accordingly), the state should return to the idle animation.
As I decided: I created a coroutine that waits 1 second and then returns the state to idle. The code:
bool isWPressed = Input.GetKey(KeyCode.W);
if ( (Input.GetKey(KeyCode.D)) && (!isWPressed) )
{
animator.SetBool("isLeftTurn", true);
yield return new WaitForSeconds(1f);
animator.SetBool("isLeftTurn", false);
}
else if ( (Input.GetKey(KeyCode.A)) && (!isWPressed) )
{
animator.SetBool("isRightTurn", true);
yield return new WaitForSeconds(1f);
animator.SetBool("isRightTurn", false);
}
else if ( (Input.GetKey(KeyCode.S)) && (!isWPressed) )
{
animator.SetBool("isBackTurn", true);
yield return new WaitForSeconds(1f);
animator.SetBool("isBackTurn", false);
}
Answer the question
In order to leave comments, you need to log in
Surely this animator has a callback to end the animation. If not, I'll be surprised.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question