Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
3
Question by Wumbo_Games · Jul 08, 2015 at 09:00 AM · animationanimatormecanimtime

How do I set the time of an animation playing in the animator (Mechanum)

I want my player to aim his gun at the cursor. To do this, I made an animation of him aiming from straight down to straight up on the animation scale of 0:00 to 1:00 (I am assuming that those are suppose to be seconds, but I am not really sure). I now want to set the time of the animation (how far along it is) so that he can go anywhere in the animation, depending on the cursor's location.

I have the angle all figured out, and I know the time I want to set it to, but I can't figure out what time variable I can change that I can get to when using mechanum, and not having a list of animation components attacked to an object. This means I cannot use animation["animation name"].time=time, in case that was not clear.

I also cannot use animator.GetCurrentAnimatorStateInfo(1).normalizedTime=time; , because normalizedTime is read-only.

I am pretty sure what I am looking for is AnimationState.time, but for the life of me, I can't figure out how to get a reference to it when I only have a reference to the animator.

If I am doing this all wrong, what should I do to make my character be able to aim at the cursor? (other than set his transform by hand, of course)

Thanks in advance!

Finally, I would like to add that this is not a duplicate of either of the following because I cannot use animation["name"].time:

http://answers.unity3d.com/questions/181903/jump-to-a-specific-frame-in-an-animation.html or http://answers.unity3d.com/questions/425985/set-the-current-time-frame-of-a-mecanim-animation.html

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

5 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by chrisholdem · Oct 24, 2015 at 02:12 AM

Chances are you've figured something out already, but this is for future viewers.

There's no clear way to access AnimationState without having an Animation component on your object.
The only alternative I've found is to use, if you cannot use legacy/deprecated options:

 Animator.Play("StateName", layer, normalizedTime);

The trick is finding a way to turn whatever value you are using into a number between 0 and 1. In my case of using a timer that is finding what amount though the time bar you want to be, then dividing it by the total time.

Hope this helps someone on their search.

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 yosske · Oct 19, 2017 at 10:26 AM 0
Share

Thank you so much, I've been looking forever for a way to directly access an animation's "time" in code without needing to revert to legacy components. You've made my day :)

avatar image Dawdlebird · Mar 26, 2018 at 09:29 AM 0
Share

Perfect, just what I was looking for!

avatar image
10

Answer by Dawdlebird · Mar 26, 2018 at 09:35 AM

For anyone looking for this as well; they've made this functionality a bit easier to access in the animator! There is a checkbox on your animation clip for 'normalized time' that uses any float variable you make in your animator as a time input. It's a more direct way to do what Chris Holdem provided a way for also. alt text


2018-03-26-11-32-04-unity-201740f1-personal-64bit.png (47.2 kB)
Comment
Add comment · Show 1 · 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 Mikael-H · Dec 20, 2018 at 10:30 PM 0
Share

This was very useful info! Now I can use a very simple class which can be called with a unity event:

 public class SetAnimationClipProgress : $$anonymous$$onoBehaviour
 {
     [SerializeField] private Animator _animator;
 
 
     public void SetAnimationProgress(float ratio)
     {
         _animator.SetFloat("progress", ratio);
     }
 }

avatar image
1

Answer by Cafetorium · Jul 30, 2020 at 01:56 PM

Referring to stuffkikker's answer, note that Normalized Time in the animation properties now seems to be called Motion Time.

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
avatar image
1

Answer by SepraB · Nov 25, 2020 at 04:33 AM

@Cafetorium is right. I had to perform the same task, aim a gun based on a number in an animated 3d model. What I did:

  1. In the animator, choose the state you need to control.

  2. In the inspector enable the property called "Motion Time"

  3. Link the 'Motion Time' toggle to an animator float parameter.

  4. From your script call the following: GetComponent().SetFloat("Float parameter name", animation expected time);

It works.

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
avatar image
-3

Answer by phaem · Jul 08, 2015 at 10:16 AM

Try to use this http://docs.unity3d.com/ScriptReference/Animator-speed.html for changing animation playback speed. It affects the current animation playing and all consequent animations so do not forget to return the value back to 1 when finished.

Comment
Add comment · Show 1 · 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 chrisholdem · Oct 23, 2015 at 09:11 PM 2
Share

I'm late to the party, but this is a completely terrible answer that completely circumvents and downplays the question he is actually posing.

He is looking to access the AnimationState.time component because he is not simply changing the rate of animation play. AnimationState allows direct manipulation of animation states, eg where the animation is within the timeline. Absolutely everyone responds in the same exact way and there is 0 progress being made in finding the solution here since using Animation has become legacy/deprecated.

Please do not respond generically to a specific question like this, it makes it look like there in an answer available when there isn't.

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

9 People are following this question.

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

Related Questions

Animator idle minimal delay 0 Answers

What is the proper way to wait for an Animator Controller to update? 1 Answer

C# Help with setting up jump animation 0 Answers

How do I stop animation from going past end time 1 Answer

Problem adding individual clip speed 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