Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by drodrii · Mar 20, 2014 at 12:56 AM · animation2dsprite

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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:

Unite 2013 - New 2D Workflows

You can define events in the dope sheet, that is, you can call a function at any moment of your animation.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image drodrii · Mar 20, 2014 at 03:39 PM 0
Share

@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...)

avatar image s-m-k · Mar 20, 2014 at 05:56 PM 0
Share

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.

avatar image s-m-k · Mar 27, 2014 at 01:25 AM 0
Share

Did I help you, buddy?

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges