• 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 Zarenityx · Mar 07, 2019 at 11:45 PM · animationanimatoranimationcliplegacyanimation

How to apply an arbitrary frame from an arbitrary AnimationClip

I am working on a project with a rather unique combat system and am running into a wall when it comes to working with animations.

Specifically, I need to be able to play any animationclip at any time within the clip. This would be similar to using AnimationCurve.Evaluate, but for an entire animation clip, rather than a single curve. I looked at AnimationClip.SampleAnimation, but this didn't seem to work. As a general example, my use case looks along the lines of:

 AnimationClip clip;
 float time = 0f;
 
 void Update(){
      time += Time.deltaTime;
      clip.Sample(time); //This is where I'd like to sample the animation
 }

Obviously, the real script is far more elaborate and involves clips playing forward, backward, stopping, or jumping to arbitrary frames. This example script is essentially just the default behaviour of an animator, playing the clip immediately.

Another thing to note is that there may be a large number of animations involved, and adding them all to an animator controller is rather impractical. As such I'd like to stay as far away from mecanim and the animator controller or playables for these as possible. I've had mixed results with the legacy animation component, but would also rather avoid that due to the potential for it to be removed (the documentation states that it is only for backward compatibility).

All in all, it appears that much of the API is based on playing clips or blending between them, but I instead wish to sample the animations manually. I don't need crossfading, layers, states, or blending for this, just simple sampling as you would with an individual curve.

I know this is probably an odd request, but if anyone knows how to do this I would greatly appreciate any pointers in the right direction.

Thanks!

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Ymrasu · Mar 08, 2019 at 12:20 AM

What I would do is use an Animator with an Animation Controller, as it seems Unity is pushing for that. You would simply load each of your clips as states, set the speed of each to zero (if you want to control them in script). A script could look something like this:

 Animator anim;
 float time = 0f;
 
 void Start() {
     anim = GetComponent<Animator>();
 }
 
 void Update(){
     time += Time.deltaTime;
     anim.Play("StateName", 0, time/lengthofclip);
 }

You would need to provide the lengthofclip, since that parameter is the percentage of the clip (zero to one). This means you could also do it by specific frame with frame/totalFrames.

Comment

People who like this

0 Show 5 · 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 Zarenityx · Mar 08, 2019 at 06:13 AM 0
Share

Unfortunately, with the massive number of small animations involved, loading them all as states is quite impractical and would be prone to human error in keeping track of all of them. I really do need to be able to load in arbitrary clips, with little to no manual input beforehand. There doesn't seem to be a good way to, say, have a single state and change the motion that uses in code, so it looks like Animator Controllers are completely out.

I appreciate the answer regardless.

In all honesty, if I can't get this to work soon, I'll probably just end up writing a custom solution. I'm only animating transforms so it shouldn't be too bad. The Animator Controller is a great tool for fluid animations, but seems to be completely lacking in the lower-level functionality that I need with this project.

avatar image Ymrasu Zarenityx · Mar 08, 2019 at 03:01 PM 0
Share

Ok, looking back at AnimationClip, clip.SampleAnimation(gameObject, time); does the same thing. But I think the time is the length in seconds and not a percentage.

avatar image Zarenityx Ymrasu · Mar 08, 2019 at 04:43 PM 0
Share

I already mentioned in the original question that this wasn't working. I'm not sure why, it just hasn't been animating anything. This method seems to be exactly the solution, although I haven't been able to find how to use it.

EDIT: Ok, so my sincerest apologies for shooting this down. I had already tried to use this method, but finally it seems to work just fine. It just needed to be in LateUpdate() rather than Update().

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

268 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

Related Questions

Some parts of animation not playing 0 Answers

how to play animation every time when the target object is found ? 1 Answer

Animations not showing for Animator 0 Answers

How to play an Animation only once from my MachineState Animator? 2 Answers

Duplicating animation clip keyframe behaviour in multiple clips 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