- Home /
How to play an animation once, then play another animation right after?
Hello everyone, so I am remaking a classic game, in which a player approaches a bat enemy and the bat is supposed to exit its idle animation, enter an animation where it unfolds its wings, then it enters a final animation where it starts flapping its wings (and then flies towards the player). I'm having trouble finding out how to play the animation for the bat unfolding its wings once and then activating the flying animation which continues to loop, and once the bat starts flying it moves towards the player (I already have that line of code for the movement). I'm not sure if I have to code the one-time animation or if I can do it only in Unity's Animation screen (I provided a pic of that below). Any help would be appreciated, thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BatController : MonoBehaviour {
Animator animator;
SpriteRenderer spriteRenderer;
public float speed; // speed that bat chases MegaMan
public float distBetween;
public float timeToAwake = 3f;
private Transform target; // holds the player
private bool isFlying;
private bool startTimer = false;
private float timer;
private void Awake()
{
timer = timeToAwake;
}
void Start () {
animator = GetComponent<Animator>();
animator.Play("Bat_idle");
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>(); // get player position
}
void Update () {
if (Vector2.Distance(transform.position, target.position) <= distBetween)
{
startTimer = true;
if (startTimer)
{
timer -= Time.deltaTime;
if (timer <= 0)
{
animator.Play("Bat_awake");
}
}
// transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime); // move bat, towards play pos, at certain speed
}
}
}
Answer by highpockets · Mar 19, 2019 at 07:36 AM
Mega Man!! Wow, classic indeed.
Choose the bat awake clip in the asset folder (not in animator) and turn off looping in the inspector (there is a Boolean checkbox).
Sadly, I already did that. Looping is turned off for the awake animation and looping is checked for the fly animation. I just realized when I play the scene, the awake animation continues to repeat itself still, so it might be because of the animation being played in Update() in the code. Do you by chance know how to make an animation play once in a script, if that's possible?
Yes, looking at your script, once your timer
bool batAwake = false;
void Update(){
if(timer <= 0.0f && batAwake == false){
batAwake = true;
//start your bat animation
}
}
Add that bool and include it in the check when you are checking your timer and it will just run the code once if you just leave batAwake to true thereafter.
Answer by Tsaras · Mar 19, 2019 at 07:46 AM
I am guessing you want to use a Transition between the 2 animations, which you can do by making a transition arrow from the animation that plays once to the one that you want to loop afterwards.
Your answer
![](https://koobas.hobune.stream/wayback/20220612193852im_/https://answers.unity.com/themes/thub/images/avi.jpg)