Play one animation after another...

Hey, so my quetion is: how do I play one animation while I press a certain input key then after the first animation ends instantly another one begins while I’m still pressing the same input key?
Thank you.

Are you using Mecanim? if not, you should, it is the animation system bundled with Unity engine. You can follow a tutorial about Mecanim here: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

One way to achieve what you want with mecanim would be:

  1. create an animator controller
  2. create a boolean parameter to know if you have the input you want.
  3. create two states in the controller for your two animations: anim1 and anim2
  4. create a transition from anim1to anim2 with exit time checked. This will be the normalized time at which the anim2 would play after anim1.
  5. in the conditions of the transition, put as condition that the input boolean parameter should be true.
  6. create a script that sets the input boolean parameters to true or false according to your characters input.
  7. you probably would need as well some other transitions from anim1 to whatever default animation you have (idle animation?) in case the input parameter is false when the condition is checked.

if you are not allowed or you dont want to use Mecanim, another solution is to use a coroutine. For this however you need at least to be able to access the current animation state (like, is the animation still playing?) or then you must know the duration of the animation. Then in your coroutine you do something like:

// this wont compile as it is
// replace with your types and methods accordingly
IEnumerator AnimationSequence(AnimationType anim1, AnimationType anim2){
	anim1.Play();

	while(anim1.IsPlaying()){
		yield return new WaitForEndOfFrame();
	}
	
	if(input.satisfied) anim2.Play();
}

// this wont compile as it is
// replace with your types and methods accordingly
IEnumerator AnimationSequence(AnimationType anim1, AnimationType anim2){
	anim1.Play();
	yield return new WaitForSeconds(anim1.duration);
	if(input.satisfied) anim2.Play();
}