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 Harry64 · Aug 20, 2013 at 06:49 PM · animationmecanimframes

Mecanim control animation frames via script?

is it possible to set the displayed frame via float?

like if a sphere is comming closer to an cube. I measure the distance to the sphere via script. if the sphere is like 10 units away it should show the frame 10 of the cubs animation. if its like 30 units away it should show the frame 30.

if its possible please tell me how I can do this. the example above is just for explanation. it would be to complicated to explane for what purpose I need the info but it would be a very important for my game and I could not do what I want to do in my game without it.

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

Answer by HeliosDoubleSix · Aug 20, 2013 at 06:59 PM

animator.ForceStateNormalizedTime(1.0);

Animator.ForceStateNormalizedTime

Comment
Add comment · Show 8 · 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 Harry64 · Aug 20, 2013 at 07:25 PM 0
Share

ok so if I have an animation that is 30 frames long and I would write animator.ForceStateNormalizedTime(0.5); then it would show the frame 15?

or did I get it wrong?

avatar image HeliosDoubleSix · Aug 20, 2013 at 07:31 PM 0
Share

that is assu$$anonymous$$g you are running at 30fps then yes, which I think is set with Application.targetFrameRate = 30; by default. It would seem wise to generally work in terms of seconds for this reason, as you may change framerate at some point deciding for a smoother 60fps.

avatar image Harry64 · Aug 20, 2013 at 07:51 PM 0
Share

it should not play the entire animtation. it should go to the frame I want to show and then stops there till I decide to show a lower frame or a higher. like the sphere moves away or getting closer. I want that the float I give mecanim shows only that frame of the animation. it would be ok if (let us say the cube has 30 frames in its animation) and I want the frame 15 to display. it should go from frame 0 to 15 and stops there and dont move on. and if I want it should move to another frame like 22 so it would go from frame 15 to 22.

avatar image HeliosDoubleSix · Aug 20, 2013 at 08:11 PM 1
Share

Can you not instantly pause it or set its playback speed to 0 or something

avatar image HeliosDoubleSix · Aug 20, 2013 at 09:35 PM 1
Share

AnimatorStateInfo currentState = animator.GetCurrentAnimatorStateInfo(animationLayer);

float playbackTime = currentState.normalizedTime % 1;

It seems like what you are trying to do with mecanim is kinda fighting with what it is designed for and maybe there is a better paradigm to use that is geared to frame/step based animation

Show more comments
avatar image
1

Answer by piacentini · May 19, 2015 at 09:06 AM

ForceNormalizedTime is deprecated in Unity 5. I am posting the simple solution I am using here in case anyone is interested:

//This will seek to the middle of the animation animator.Play("yourcurrentstate", -1, 0.5f); animator.speed = 0f;

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 TimothyAllen · Oct 11, 2015 at 12:23 PM 0
Share

@piacentini animator.Play() doesnt work properly when used in State$$anonymous$$achineBehaviour scripts while the ForceStateNormalizedTime does... I can't seem to find a solution to control animation keyframes from a State$$anonymous$$achineBehaviour.

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

18 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

Related Questions

How to animate a property from current value to a given value? 2 Answers

Mecanim - Rotate Generic GameObject and play Animation 0 Answers

Can i use mecanim animator while using legacy? 1 Answer

Animation on 'extra' bones missing/stripped in Unity. (Unity 4.6) 0 Answers

Retargetable animation 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