Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TysonG · Mar 19, 2019 at 03:50 AM · animationanimationslooping

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. alt text

 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
         }
     }
 }


2019-03-18.png (361.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TysonG · Mar 19, 2019 at 02:02 PM 0
Share

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?

avatar image highpockets TysonG · Mar 19, 2019 at 08:04 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

266 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turn based game animation through mechanim 1 Answer

Animation not playing correctly 1 Answer

About mecanim and changing animations, there must be more clever way of doing this. 0 Answers

Is it possible to create new humanoid clips within Unity? 2 Answers

Dynamically add AnimationState to Controller 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges