- Home /
How to make an animation play after releasing key?
Hi, so this might be a noob question but I'm finding it hard to transition from a walk state into an idle with an intermediate clip of the character settling into the idle state. Basically the character walks and when you release the button, the idle plays immediately. How would I go about making it so that when you release a key the character plays a settling animation automatically before the idle?
Answer by camdenlink7 · Jun 19, 2019 at 02:21 PM
This is completely untested code of the top of my head, but hopefully it is a start. You may have to make minor tweaks, for example you may not have to uppercase "Play", I am not sure. I hope this helps some!
public Animator animatorOnPlayer;
public Animation settlingAnimation;
public Animation; idleAnimation;
void Update()
{
if (playerIsWalking)
{
if (Input.GetKeyUp("walkingKey"))
{
animatorOnPlayer.Play(settlingAnimation);
StartCoroutine("wait");
}
}
}
Ienumerator wait()
{
yield return new WaitForSeconds(settlingAnimation.length);
animatorOnPlayer.Play(idleAnimation);
StopCoroutine("wait");
}
Is StopCoroutine necessary in this case? There's no loop in there or something so the routine will stop automatically I think ?
Your answer
Follow this Question
Related Questions
Animation not at correct position W/Video 0 Answers
Distribute terrain in zones 3 Answers
Animation Not Playing - VideoExplanation 2 Answers
How do I know when an object is going under a Platform Effector 2D object? 0 Answers
Animation or programming? 2 Answers