• 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
Question by Chocolade · May 08, 2017 at 02:50 PM · c#animationscripting problemanimatorscript.

How can i use the Animator in script to find when animation clip finished playing and then stopping the animation ?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AnimationCamera : MonoBehaviour
 {
     public Camera animationCamera;
     public Camera mainCamera;
     Animator _anim;
     AnimatorStateInfo currInfo;
     float animationnormalizedTime;
 
 
     private void Start()
     {
         animationCamera.enabled = false;
         mainCamera.enabled = true;
         _anim = GetComponent<Animator>();
 
         currInfo = _anim.GetCurrentAnimatorStateInfo(0);
         animationnormalizedTime = currInfo.normalizedTime;
     }
 
     private void Update()
     {
         if (currInfo.length == animationnormalizedTime)
         {
             _anim.CrossFade("Animation_Idle", 0);
         }
 
         if (Input.GetKeyDown(KeyCode.C))
         {
             animationCamera.enabled = !animationCamera.enabled;
             mainCamera.enabled = !mainCamera.enabled;
 
             if (animationCamera.enabled)
             {
                 _anim.CrossFade("Animation_Sign", 0);
             }
             else
             {
                 _anim.CrossFade("Animation_Idle", 0);
             }
         }
     }
 
     public void PlaySignAnimation()
     {        
         animationCamera.enabled = true;
         _anim.CrossFade("Animation_Sign", 0);
     }
 }

I'm trying to use the currInfo. I want to find the animation clip length and also how much time currently passed since the animation clip started. Then to compare the values and in the end to make it "Animation_Idle".

Sub question: "Animation_Idle" is a state i created in the Animator window so it will not play anyt$$anonymous$$ng. But maybe it's the wrong way to stop animation using Animator. Should i use somet$$anonymous$$ng else to stop the animation instead using "Animation_Idle" ?

Comment

People who like this

0 Show 3
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 Masterio · May 08, 2017 at 05:22 PM 1
Share

First You should use the transitions between an animation clips in Animator editor. So you dont need to check if animation ends.

When you make transition (arrow) between the 'sign' and the 'idle' it will be enough. Next make sure the 'sign' clip is not looped simply click on clip in Project and uncheck 'Loop Time' . Now when you run 'sing' animation (with animator.Play("animation_sign");) it will automatically skip to the 'idle' animation when it ends.

avatar image Chocolade Masterio · May 08, 2017 at 06:12 PM 0
Share

Ok i did it and it's playing it once now. But then in the end it's not getting back to the Main Camera. I did in the script enable true to the animationCamera in the PlaySignAnimation method:

 animationCamera.enabled = true;

So now when it finish playing the clip i'm getting this in the game view: It's not the main camera and not the animationcamera. Somehow i need in the script to make that when it finish playing the animation clip change the animationcamera enable to false and make the main camera enable to true.

Gameview

I checked the Animator window. It start when the game start on idle then it get to the animation clip state when finish it back to the idle state. The problem is that nothing tell the cameras to do the switch back when it finish playing.

gameview.jpg (64.4 kB)
avatar image Masterio · May 08, 2017 at 06:44 PM 1
Share

Click the 'AddBehaviour' button on Animator->'Animation_Sign' it will be placed in inspector. Then open created script and attach this code:

 private AnimationCamera _animCamera;        // handler
 
 override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 
 {
     if(_animCamera == null)
     {
         _animCamera = animator.GetComponent<AnimationCamera>();
     }
     
     if(_animCamera != null)
     {
         _animCamera.RunCameraDetachMethod();
     }
 }


It requires add to your class public method to execute when animation ends. it will works if AnimationCamera is placed on the game object with the Animator component.

You can read about state machine behaviours on: https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by FM-Productions · May 08, 2017 at 06:03 PM

Hi,

I t$$anonymous$$nk your script is already on the right path, but none the less, I try to give a complete answer to the question below. If you have a transition "On Exit Time" set from your current state, you could simply check when the current animation state name is not your previous animation anymore. Else you can work with the normalizedTime, like you already do in your script.

You can get the current state info with Animator.GetCurrentAnimatorStateInfo. If called with a parameter 0, you should get the currently active animation state: animator.GetCurrentAnimatorStateInfo(0) where animator is the variable that stores your Animator. You can then proceed to check the animation you are in, for example by comparing the name of the current animation:

 if (animator.GetCurrentAnimatorStateInfo(0).IsName("Animation_Idle")){ }


You can get the time of the current state via the normalizedTime, t$$anonymous$$s is a float value between 0 and 1 where as 0 is the very start of the animation and 1 is the end of the animation. T$$anonymous$$nk of it as how many percent of the animation has been played. for example a normalizedTime of 0.34 means that 34% of the animation has been played. Without testing it, but after looking in the Unity docs, I t$$anonymous$$nk you can access the normalizedTime of the current animation like t$$anonymous$$s.

 animator.GetCurrentAnimatorStateInfo(0).normalizedTime 



Somet$$anonymous$$ng I noticed about your script, at line 26: Do not do t$$anonymous$$s:

 if (currInfo.length == animationnormalizedTime)

Instead, check if the value is above a threshold instead of an exact value. Also, let's say you want to switch from "Animation_Idle" to another state, do t$$anonymous$$s instead (you don't want to call your animation switch function in every animation state or do you?). Also, forget currInfo.length, set the normalizedTime, when you want to exit as a separate variable.

 if (currInfo.IsName("Animation_Idle") && currInfo.normalizedTime >= customSetNormalizedTime)

The last t$$anonymous$$ng is, I t$$anonymous$$nk you have to call currInfo = _anim.GetCurrentAnimatorStateInfo(0); in your Update() function, not the start function. Else I believe you will have the same currInfo in every update (the currInfo you got in the Start() function), but you want to have the currInfo of the current frame.

However, for switc$$anonymous$$ng between animation states, I t$$anonymous$$nk setting up transitions in the Animator itself is preferable to control everyt$$anonymous$$ng from script. Then maybe set variables in your animator and transition conditions for animation states that depend on the variable values. You can then communicate with the Animator in the script. If you are trying to learn Unity's animation system or if it still feels unfarmiliar to you, and if you have the time, maýbe check out t$$anonymous$$s video:

https://www.youtube.com/watch?v=wdOk5QXYC6Y

Also, if you don't know how to get a certain value from the Animator or other classes, make sure you check the API docs first, since they describe every possible function and method you can call on certain Unity classes:

https://docs.unity3d.com/ScriptReference/Animator.html

If hope I could help you!

Comment
Chocolade
GameDevBryant
Malghull
GamesOfArcadia

People who like this

4 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 TomArano_Dimenco · Jan 20, 2020 at 09:40 AM 0
Share

i've tried to get 'normalized time' from an animation but it does not go from 0 to 1, it keeps counting up (indefinitely as far as i know)

avatar image AconitumNapellum TomArano_Dimenco · Jan 20, 2020 at 10:23 AM 0
Share

That's why you check if normalizedTime is >= 1. normalizedTime doesn't stop "counting" when animation has finished playing, because if you had a loop you could know how many times the animation played.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Why the speed parameter in Animator change the animation speed only for door open but when the door is closing the speed still very fast ? 1 Answer

Why when creating new animator controller for the character the character is not walking right ? 0 Answers

How to check if Animator is playing 3 Answers

How to stop animation from playing in c#. 7 Answers

Is there a new retargetting system for the animation in 5.5? 1 Answer


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