• 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 hakermania · Jul 26, 2013 at 10:04 PM · animationanimationstate

How to access specific animation state as animation

This isn't so complex as it sounds. I have an animation Component and I want to wait till an animation that is playing to stop. This is my code:

 function WaitForAnimation ( animation : Animation)
 {
     yield; while ( animation.isPlaying ) yield;
 }

 ...
 ...
 hit.collider.gameObject.animation.PlayQueued(animationToPlay);

 WaitForAnimation(hit.collider.gameObject.animation[animationToPlay]);

The thing is that hit.collider.gameObject.animation[animationToPlay] is of type AnimationState and while I need to wait for the specific animation to stop playing. There are 5 or 6 animations attached to my gameObject and I don't really care which one is currently playing, I just need to wait for the current playing animation to stop.

I tried with simply putting WaitForAnimation(hit.collider.gameObject.animation); but the function exits immediately, without really waiting for the animation to finish.

What I am trying to do is the following:

I have 4 separate animations. Each one of these rotates the object 0->90 or 90->180 or 180->270 or 270->360 (that's why 4 different animations).

When the object for some reason needs to be animated from 0->90 and then immediately from 90->180, the 0->90 animation doesn't manage to complete to its time. So a hiccup is being observed from x (where x is between 0 and 90, and represents the 1st animation's angle when the 2nd animation started being rotated) to 90, which is the 2nd animation's starting angle.

So, that's why I used PlayQueued() so as to wait for each animation to finish before the next to start.

But, I have problems with the accuracy of it. For example, if I play the 1st 3 animations simultaneously (with PlayQueued()) and wait for all to finish, I observe angles of 271,1234 and such. That's why I wanted to have WaitForFinish, so as to manually fix the angles of the objects.

But, if there is a way to fix the PlayQueued accuracy issue, it would be preferred.

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
1

Answer by Paulius-Liekis · Jul 27, 2013 at 06:18 AM

Try printing weight and enabled properties of hit.collider.gameObject.animation[animationToPlay] in WaitForAnimation and see what's happening. Maybe animation start is delayed by one frame (although it looks like your code should work fine in that case).

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 hakermania · Jul 27, 2013 at 07:02 AM 0
Share

Well, thanks for the answer! I guess that the animation doesn't start. And indeed, enabled is false (!). I think it is PlayQueued to blame. When I use plain Play the code works, but it doesn't suit me.

avatar image hakermania · Jul 27, 2013 at 08:57 AM 0
Share

I added extra information on the original post about the initial problem that I was trying to fix this way.

avatar image
1

Answer by RyanZimmerman87 · Jul 27, 2013 at 08:36 AM

I'm not sure if there's a better way to do it but you could try something like:

 bool animationActiveBool = false;
 
 void Update()
 {
 
 //animation condition occurs then
 animation.Play ("Recoil");
 animationActiveBool = true;
 
 if (animationActiveBool == true)
 {
 if (!animation.IsPlaying("Recoil")
 {
 animationActiveBool = false;
 }
 }
 
 if (animationActiveBool == false)
 {
 //logic for next animation sequence
 }
 
 }


That's an example of the way I might handle this kind of problem, but you might need multiple bools for different animation states and then it's just a matter of setting up the flow of logic so everything works nicely.

I'm not sure if that will help though it sounded like you were looking for a simpler alternative, I'm not sure if one exists.

Comment
Add comment · Show 6 · 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 hakermania · Jul 27, 2013 at 08:38 AM 0
Share

Thanks for this answer. What if the animation is queued? Will isPlaying() wait until its row has come and finished playing completely? I think that, according to the documentation, no (http://docs.unity3d.com/Documentation/ScriptReference/Animation.IsPlaying.html)

avatar image RyanZimmerman87 · Jul 27, 2013 at 08:46 AM 1
Share

I'm not exactly sure because I've never used PlayQued or WaitForAnimation.

If I'm understanding what you are trying to do correctly than I believe that you could achieve pretty much identical results without actually using those features. You can easily set up a sequence of animations waiting to start once the last one finished by using some combination of bools or even an int to deter$$anonymous$$e which animation should play next.

$$anonymous$$ight be a little bit annoying to program depending on what the object is doing and what animations need to play, but that's the way I've had to handle it so far.

avatar image hakermania · Jul 27, 2013 at 08:49 AM 0
Share

Argh, that sounds difficult in my case, because the animated objects are not 1 nor 2.. but 32. So I will have to setup a Class to hold these values you describe for each of the objects. I'm targeting mobile platforms as well, so that sounds heavy.

avatar image hakermania · Jul 27, 2013 at 08:57 AM 0
Share

I added extra information on the original post about the initial problem that I was trying to fix this way.

avatar image RyanZimmerman87 · Jul 27, 2013 at 09:42 AM 0
Share

Hmm that does sound quite complicated, one thing you might want to try is using animation.Crossfade ins$$anonymous$$d of animation.Play if the animations have a visual hiccup. I don't think that would solve the problem of getting strange angles like 271 though.

Hard for me to say what the problem is since I never used those features. If all the animations do is rotate the object on one axis you may want to consider using some kind of Quaternion slerp function to rotate the objects to look at the destination points in a specified speed and ti$$anonymous$$g. This would probably be my last resort though as it would likely be more difficult to set up then getting the animations to work correctly.

"When the object for some reason needs to be animated from 0->90 and then immediately from 90->180"

That is the part that seems to be cause of all these problems? Crossfade might be able to help it look better but I don't think it would solve it. I think you are going to either have to create a new script which controls the animation logic for each object kind of like my example above. Or you could not allow an animation to play unless it's rotation angle is already 90, 180, 270, or 0.

From what I can tell the easiest solution would be to use something like my example above where 0 degrees could be:

int animationState = 0;

//90 degrees animationState = 1;

//180 degrees animationState = 2;

//270 degrees animation state = 3;

So you could use this int to track when the animation is finished playing and when it does so the animationState would change to the next number. Only then would the next animation be able to play.

Hope that helps sounds a little complicated it's possible I'm missing some important details of your project but I think this could work.

Show more comments

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

17 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

Related Questions

Using animationState for animation controls 2 Answers

Accessing AnimationState in Animator 0 Answers

How to Play an animation state on an animator controller on collision? 1 Answer

Toggling between states of animation 1 Answer

How to stop an default animation from playing? 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