Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BrianS. · Jun 05, 2013 at 05:26 PM · animationanimationclipclip

Trying to check what anim clip is currently running on object

I have an object with several animations attached to it in the animator. I'm setting up the flow, and things are going good, but I need set a variable based on what animation is playing, or a way to tell what one I'm in.

thought this was the way to go.. but maybe not?: (In my class)

private Animator anim;

private AnimationInfo animInfo;

anim = GetComponent();

Then I thought I could do this:

animInfo = anim.GetCurrentAnimationClipState(0);

if(animInfo.clip == "spin")

But I get an error on the GetCurrentAnimationClipState because it's an AnimationInfo[] and not just AnimationInfo

Maybe I'm doing it all wrong? Anyone have a suggestion for a few lines that will get me the clip name that's running that I can check against?

thanks in advance! -B

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TonyLi · Jun 05, 2013 at 06:07 PM

Mecanim can blend animations on a layer -- for example, running forward + turning to create a running turn. So multiple animations can be playing at the same time.

If you absolutely know that only one animation is playing, you can just use AnimationInfo[0]. But it would probably be better to iterate through the entire return value.

If you want the animator state, see: http://answers.unity3d.com/questions/418854/getting-a-list-of-mecanim-states-in-animator.html

Comment
Add comment · Show 4 · 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 BrianS. · Jun 05, 2013 at 08:12 PM 0
Share

Thanks. I think I'm closer.. but still not there. So, I'm sure I'll be only running the 1 animation at a time. I'd gladly iterate through whatever needed. I'm apparently brain dead today, and not sure how to get there. Looking at the Animator, in the Base Layer, I'm wanting to get what clip I guess the transition or the clip from the controller. so Base Layer.animName is what I'm looking for, and just can't figure out how to get there. I'm able to set Parameters, and force transitions, but I need to know where I'm at in my controller in a script. Thanks again for your help, but any more examples/info would be greatly appreciated. going to grab some caffeine, maybe that will help!

avatar image TonyLi · Jun 06, 2013 at 01:42 AM 0
Share

Unity recommends a simple "control" layer on top of Animator to keep track of things like this. (See: http://docs.unity3d.com/Documentation/$$anonymous$$anual/$$anonymous$$ecanimPeformanceandOptimization.html) I agree. It gives you more flexibility, and also future-proofs your code against the possibility of multiple simultaneous animations.

This layer can simply get/set one or more animator controller parameters, or you could make it more elaborate. You might consider an enum for the current state of the layer. For example, a snippet might look like:

 enum CharacterState { Idle, Walk, Spin }
 private CharacterState state = CharacterState.Idle;
 
 private void StartWalking() {
     state = CharacterState.Walk;
     animator.SetFloat("Speed", 1);
 }

 private void StartSpinning() {
     state = CharacterState.Spin;
     animator.SetBool("Spin", true);
 }
 
 if (state == CharacterState.Spin) {...}


But if you really just want the animation clip name, the easiest solution is to make sure your animation state name matches the clip name. This is the default when you drag an animation clip into the Animator Controller edit pane.

 Animator animator = GetComponent<Animator>();
 int currentClipHash = animator.GetCurrentAnimatorStateInfo(0);
 int spinHash = Animator.StringToHash("spin");
 if (currentClipHash == spinHash) {...}

(Warning: untested code)

avatar image BrianS. · Jun 06, 2013 at 10:14 PM 0
Share

Thank you for the info and help! Very informative. The reason I started this way is because I was going the enum route. So here is the situation. Animator starts up.. and I'm in my Idle Animation. And I set my state to CharacterState.Idle So then I want to do a spin.. so I get a keypress and call StartSpinning() just like you have above.

After my spin is done (transition on exit time) I go back to idle.

$$anonymous$$y problem is, state is still equal to CharacterState.Spin and my bool of "Spin" is still true. So the reason for my original question: I want to know when I'm in the spin animation, so I can set the Spin to false, and my state back to idle when I get there. I just don't know when that happens. I was hoping to know when my Spin animation was happening, so I can set my state and bools etc. Else I keep ping-ponging back to spinning because my transition sees Spin as true still.

Is there a better way I should do that? How does everyone else re-set their animator.Bools to false, or whatever and know when to do it? Thanks again!

avatar image TonyLi · Jun 07, 2013 at 01:28 AM 0
Share

Use the Event System for $$anonymous$$ecanim. I'm not the author, and I have no stake in plugging it, but it's more than worth the cost. You can just attach an event to the end of your spin animation to call some method like StopSpinning() or StartIdle().

Or, if you don't want to get the Event System for $$anonymous$$ecanim, you can do like the Unity-provided examples and check GetCurrentAnimatorStateInfo() in every Update() cycle (like the example code I posted above).

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

15 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

Related Questions

Possible to play single clips using Mecanim? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to create animation clips? 4 Answers

Empty animation clips when copying to Prefab 1 Answer

Remove all clips from animation by script 2 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