- Home /
Answer by save · Nov 01, 2011 at 01:52 PM
You use AnimationState.time to jump to a specific time in an animation.
animation["MyAnimation"].time = 5.0;
Thanks, that works. How would I stop the animation on the chosen frame. Using animation.Stop after the code you provided gives different results with every try. Thanks
This is a pretty old comment, but I came across a better solution and thought I should post it. You don't have to call this every frame, just keep the speed of the animation at zero:
animation["$$anonymous$$yAnimation"].time = 5.0; animation["$$anonymous$$yAnimation"].speed = 0.0; animation.Play("$$anonymous$$yAnimation");
Hello from 2019. Link is broken in this reply. Here you go: AnimationState.time
Answer by ensomniac · Apr 18, 2013 at 08:02 PM
Since this is the first post that comes up on Google, here is a slightly more complete answer:
var desired_play_time = 2.3;
animation["MyAnimation"].time = desired_play_time;
animation["MyAnimation"].speed = 0.0;
animation.Play("MyAnimation");
Don't forget you could also use: animation[ myAnimationClip.name ].time = .....
if you don't want to provide hard-coded string
Answer by ColinKnueppel · Feb 04, 2014 at 02:57 PM
You can change the time of an animation inside an animator by using the: animator.Play("animName",layer int, NormalizedTime float);
A note: "animName" is actually the named anim state in the animator, so whatever you name it, is what it will call. It also can cause the current animation to jump to this named state. I haven't found a way to access animations inside blend trees, but you can use this to jump to frames on the total blend tree.
I just tried what Colin said and can confirm it works. I was stuck on trying to jump to frame using an animator but this did the trick. Thanks!
@colin I think your answer is correct if we are using mecanim and unity4.3
Took a while to find this one, but it is simple. Even works in conjunction with Animator.crossfade. Just set speed to zero, and you have a "frozen" animation.
I'm confused on where to get the animName from. I am in my animation and using and event call back to have my animation jump to one of three points in the animation at random. So in the Animator how do I get the current animation state name and how do I get the current layer I am on?
Answer by VillageID_10t · Jan 26, 2015 at 09:27 PM
You really shouldn't need to muck with the speed of the animation
AnimationState clipState = animation.GetClip( "idle" );
clipState.normalizedTime = Mathf.Random( 0.0f, 1.0f );
animation.Play();
animation.Sample(); //forces the pose to be calculated
animation.Stop(); // actually commits the pose without waiting for end of frame
AnimationState clipState = animation.GetClip( "idle" ); //error
Can't convert AnimationClip to AnimationState
important remark about anim.Sample() helped me, thanks!
Answer by Noob_Vulcan · Sep 18, 2015 at 06:45 AM
Well if you want to do that in Animator then use this
Lets say you want to play the animation from the mid frame .This is how u will do it
getComponent().Play(“Animation_Name”,0,0.5f);
or
getComponent().Play(“Animation_Name”, 0 , (1/total_frames)*frame_number);
For more you can refer here http://www.unityrealm.com/play-animation-from-frame-unity-5-animator/
Hi, how can I reset an animation? $$anonymous$$g. I’ve got a animation playing and the user can trigger to exit the animation at any time. I then want the animation to reset, so it is at frame 0.0f again.
Background: I'm creating an Augmented Reality App. So if the tracking is active objects are visible. After some time (e.g. 5 seconds) the animation starts to play. Then if the user looses tracking the objects are hidden. If the uses finds the tracking again, the objects are visible. Now the problem is that the animated object is still somewhere in the animation where the user left before. But I need it to be at the beginning again.
Thanks
Hi , Simply Deactivate your gameobject when they are not visible (right now your renders are deactivating ,not the gameobject ) .
Re activating gameobjects rests the animations .
Or you can use
_animator .Play(“same state you are”, 0, 0f);
I'm deactivating my gameobjects in my AR project , and it works fine
Thanks for the reply. But it doesn't work for me. Not sure if I understand you correctly. I tried:
myAnimatedObject.SetActive(false);
when tracking is lost and the scene is reset. Then I do
myAnimatedObject.SetActive(true);
when tracking is active again. The object is shown, but the animation is still where it ended when it was interupted.
I also tried
anim.Play(animName, 0, 0.0f);
anim.speed = 0.0f;
but that also didn't help. $$anonymous$$aybe I'm doing something wrong with my animator setup? Please check the image of my setup.
Thanks! BTW: What AR sdk do you use? We're working with vuforia. From what company are you? What did you develop already? :-)
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How can i make my 2D character not toppling over 0 Answers
Animations on a gun 1 Answer
Stop one animation while another plays 2 Answers
code to play my animation on button press, but it is looping. What do I add to stop this? 1 Answer