- Home /
Running 2D Sprite Animation Once?
Hello everyone I hope you're doing fine.
So, Im running a simple animation on an Update Function.
int index = (int)(Time.timeSinceLevelLoad * framesPerSecond);
index = index % spritesMovement.Length;
spriteRenderer.sprite = spritesMovement [index];
It runs smoothly.
Now my problem is this, I want to destroy that object but I want it to run one animation of 4 frames and then destroy the object. Currently Im not being able to accomplish, what should I do?
The current obstacles.
Update is running constantly, Im guessing a bool with an if on the update.
How can I run an animation once on 'X' time(speed of animation) and THEN(because as steps go fast) destroy the object.
What I did to counter the "THEN destroy object" was something like
private IEnumerator destroyWithDelay ()
{
yield return new WaitForSeconds (0.5f);
Destroy (this.gameObject);
coroutineStarted = false;
}
But im really spaced out on how to tackle this.
Thanks for your help and time I really appreciate it!
Answer by s-m-k · Mar 20, 2014 at 01:22 AM
Perhaps try counting it differently?
float timeRecordedOnStart;
void Start() {
timeRecordedOnStart = Time.time;
}
void Update() {
int framesProcessed = (int)((Time.time - timeRecordedOnStart) * framesPerSecond);
if (framesProcessed >= spritesMovement.Length) {
Destroy (this.gameObject);
}
int index = framesProcessed % spritesMovement.Length;
spriteRenderer.sprite = spritesMovement [index];
}
That should fix your issue for now, but I suggest you use the native Unity animations. It's pretty simple in the current Unity, and that video explains/advertises it pretty well:
You can define events in the dope sheet, that is, you can call a function at any moment of your animation.
@s-m-k Do you happen to know any tutorials on how to do this? I understood most of the video, but where would I call these animations && such? (Im a newbie when it comes to unity, I tend to code everything...)
Basically just open the Animation View: click on Window > Animation. Create a new, empty GameObject and select it. Then select multiple sprites and drag them to the Animation View. You'll be prompted to save new animation clip. If you do it, your first animation will be ready. You can then test it by pressing the Play button (that triangle in the top left corner of Animation window).
If you wonder how to call functions from your animation, check out Unity - Using Animation Events. All you need is to define a function in a script attached to your animated object, it'll be automatically visible in the editor.
I've never used any tutorial myself, but this one looks pretty good (step by step): http://www.youtube.com/watch?v=O_0gZuqW6y8
If you prefer text tutorials, here's one: http://totallysweetredhoodie.blogspot.com/2013/11/tutorial-using-unity-2ds-new-dope-sheet.html
The last one shows how to animate transforms rather than increment pre-drawn frames, but it's pretty much the same principle. Just keep in $$anonymous$$d that you can animate almost everything.
If you need more explanations, feel free to ask.
Answer by corriedotdev · Mar 20, 2014 at 10:26 AM
I would float the animation for like 0.35 seconds then trigger the destroy gameobject
Your answer
Follow this Question
Related Questions
How do I stop sprite animation on last frame? 2 Answers
Most efficient way to do 2D animation 2 Answers
Anima2d: How to use a single sprite for two paws? 0 Answers
SpriteManager 2 1 Answer
Sprite animation 2 Answers