- Home /
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.
Answer by HeliosDoubleSix · Aug 20, 2013 at 06:59 PM
animator.ForceStateNormalizedTime(1.0);
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?
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.
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.
Can you not instantly pause it or set its playback speed to 0 or something
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
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;
@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
Follow this Question
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