What is the correct way to do this? Lets say you have a object with two doors on it. By default on startup these doors are both closed. During runtime you can go open up both doors individually. If you leave them both open but would like to have a button on screen to reset these doors back to close, what is the most effective way to go about this. I have tried playing both of their door close animations but the problem here is that since these doors are children of the same object, the first doors animation is called, then the 2nd and only the 2nd plays. The first is stopped by the 2nd one starting. This workaround is my artists mindset kicking in hehe
I found this thread on the subject but i dont understand whats going on exactly. Or how to take this and mold it to what i need.
Is there a more simplified piece of the animation code that can be used to reset the current frame of animation? I know the frames of each objects animation slider obviously, so maybe if i could just enter in the specific frame i would like them to be on that could work?
after searching for a while longer i finally found a thread that closer fits what i need, hopefully.
im hoping SampleAnimation can do this job i need. I just do not understand the usage on it. I know this is a dumb question but i dont understand how to use this code.
Here is the reference from the Script Help.
function SampleAnimation (animation : AnimationClip, time : float) : void
// Plays an animation clip backwards by sampling it every frame.
var clip : AnimationClip;
function Update() {
gameObject.SampleAnimation(clip, clip.length - Time.time);
}
What i dont understand is why you need to make a variable called clip? Using this just goes right over my head at this point.
gameObject: This will just be the name of the object in my Hierarchy that holds the animation correct?
SampleAnimation: This is what i want to do with gameObject
clip: this is the variable made at the start of the script, but why its here i dont get. Why cant i just enter the name of the animation i would like to use thats part of my game object? I dont want to make a new clip,i have one already made when i imported the object.
4.clip.length: I get that this is how long the animation takes to play back, but why is this needed and how do i calculate this. I just want to set it to the first frame.
These are very basic questions but the logic behind some of these is hard to grasp at times. If you could offer some help i would really appreciate the clarification on this. Thanks
I’ll have to jump on the same train here as I can’t get SampleAnimation to work either.
I’m trying to simply play a 30 frame animation once. Wait for about 5 seconds and then reset the animation back to the first frame so it looks the way it did prior to playing the first time.
If I have a group of objects, in which the parent has one animation that rotates one of the child objects but its triggered by the child:
on childObj:
function OnCollisionEnter( collision : Collision ) {
[...] // some other code
transform.parent.GetComponent("AnimationScript").aniStart = true;
}
on parentObj (AnimationScript):
var aniStart: boolean = false;
var aniReset: boolean = false;
var aniTime: float;
function Update () {
if (aniStart) {
// boolean is true -- play animation
animation.Play();
aniStart = false;
aniReset = true;
aniTime = Time.time + 5.0;
}
if (aniReset (Time.time >= aniTime)) {
// boolean is true and current time is 5 seconds later
gameObject.SampleAnimation(animation.clip, 0);
// go to frame 1
}
}
BTW – this is not copied code; I keyed it in so there may be a typo – but you get the point.
[Edit]:
I figured out that SampleAnimation is not asking for the Frame number, but instead asking for the time in seconds(float) to go to. When I used 1, it meant 1 second which was the whole length of my animation. I changed this to 0 and it worked. I have no idea why I interpreted this as a frame number. Perhaps because that is what I was targeting.
Is there any way to specify which frame number you want to go to and stop or play from?
You can use Animation.Rewind to reset the animation back to the start immediately. If you want to play the animation in reverse, set its speed value to -1.
Depending on how you call Play(), you may want to hash the animation to prevent too much GC. Tho, “SettingsOpen” probably isn’t looped in any way.
If you are able to access an animation directly from the AnimatorController (ex: Play(int hash)), then we should also have:
Stop(int hash). Stops the given animation from playing. … is this the same as StopPlayback?
Get(int hash). Returns the Animation object.
Rewind(int hash). Or, better, SetPosition(int hash, float x). Sets the Animation back to a time location.
Also, it would be valuable to add in default events on Animations, since they are very callback driven. AnimationStart and AnimationComplete would be good ones.