- Home /
How to start the animation of a duplicate object on a different frame
Hi everyone!
Let's see if I can explain myself clearly:
I have a gameObject with an animation of 10 frames attached to it. I need to duplicate the object several times, and I would like to alter the animation on each of the duplicates so the animation starts one frame ahead.
so the original would start at frame 0, the first duplicate at frame 1, the second duplicate at frame 2, and so on.
Is there anyway to do this, without having to create several different animations, or, if possible without having to create a single script for each of the duplicates?
If not, how could I do it anyway? I've been having trouble finding a way to actually control the animations frame by frame.
Thank you in advance
Answer by Saad_Khawaja · Jul 16, 2014 at 10:56 PM
Unity does not have a method to goto a specific frame per say. But you can move to a specific time interval of an animation using Gameobject.animation["name"].time - Legacy Animation
So in short, you can actually goto a specific frame by finding the time of the animation at that particular frame. e.g. If the animation speed is 24 fps: It means 24 frames are rendered in 1 sec.
The first frame will be at 1/24 1. The second will be at 1/24 2. and so on.
So this snippet will do the trick:
gameobjectname.animation.Play ("Take 001");
gameobjectname.animation.Stop ();
gameobjectname.animation["Take 001"].speed = 0; //to make the animation pause
gameobjectname.animation["Take 001"].enabled = true;
gameobjectname.animation["Take 001"].time = (1f/24f)*frameNumber;
Just to add, you can find the speed of the animation in the animation window. (The samples count if i'm not wrong)
Thank you for your answer, that seems like it will do the trick.
I guess it also means I will need to have a separate script for each of the duplicates (which will add up to several dozen)... it won't look particularly good, but if it's the only way I'll do it :)
Why do you need to have a separate script for each duplicate?
You can easily create a function and use that to instantiate and set respective frames: for e.g.
void create$$anonymous$$ultipleObjects()
{
for(int i=0;i<10;i++)
{
GameObject tmpObj = GameObject.Instantiate(Object1,Vector3.one,Quaternion.identity) as GameObject;
setFrame(tmpObj,i+1);//Set Frame Number as i+1 so it ranges from 1 to 11 for eg.
}
}
void setFrame(GameObject gObj, int frameNumber)
{
gObj.animation.Play ("Take 001");
gObj.animation.Stop ();
gObj.animation["Take 001"].speed = 0; //to make the animation pause
gObj.animation["Take 001"].enabled = true;
gObj.animation["Take 001"].time = (1f/24f)*frameNumber;
gObj.animation.Play ("Take 001"); //Start animation from specified frame again
}
Yes, you're right. And I like your idea. But I don't think you should make 12 scripts when one can do the job:
You can use a public variable frameNumber and then change it for any GameObject that you attach it to.
For e.g.
public int frameNumber;
void Start()
{
gameObject.animation.Play ("Take 001");
gameObject.animation.Stop ();
gameObject.animation["Take 001"].speed = 0; //to make the animation pause
gameObject.animation["Take 001"].enabled = true;
gameObject.animation["Take 001"].time = (1f/24f)*frameNumber;
gameObject.animation.Play ("Take 001"); //Start animation from specified frame again
}
Okay, with sprite animation its totally different : I would suggest the following: (Do not make animation using animator component/$$anonymous$$ecanim. Remove it as you won't need it)
Download this SpriteAnimator.cs script by Luis Santos.
forum.unity3d.com/attachments/spri$$anonymous$$nimator-cs.104362/
Attach this script to your Coin game object
Fill the inspector values - Attached
Your answer
Follow this Question
Related Questions
Duplicate Animator Controller problem - models still using the previous controller 1 Answer
Determine in which frame is an Animation currently 2 Answers
[SOLVED] AnimationState.time doesn't work and returns NullReferenceException error 1 Answer
How to change the intervals between frames in the animation window? 1 Answer
2D Game change object's position based on animation. 1 Answer