Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Endesmomo · Mar 01, 2017 at 11:30 PM · animation2dspriteanimatorframe

How to reuse frames in a 2D animation?

This is my sprite sheet, when I select the frames and drag them into the scene view to create the animation, it just creates an animation with the selected frames in sequence and I can't figure out how to change that sequence. If we numbered the frames from left to right and top to botom, I'd need the sequence to be: 2-3-5-2-4-6- and loop.

I can just repeat the frames in the intended sequence, but there must be a smarter way to do this.

alt text

screen-shot-2017-03-01-at-202247.png (11.0 kB)
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
1
Best Answer

Answer by Commoble · Mar 02, 2017 at 02:29 AM

Personally, I think the smarter way to make 2D animations is to throw out the existing animation system and animate them manually. In a script on your object, declare public SpriteRenderer render so you can get a reference to the SpriteRenderer in the Inspector and set its sprite via a script. Then, declare an array of sprites (you can do this in the same script for now, but this is also a good place to use ScriptableObjects and you may want to read up on these) and put all your sprites in there.

Now you have a reference to each sprite, so you can animate the SpriteRenderer by setting its sprite to whatever it needs to be in whatever order you need. In an Update function, Time.deltaTime gets the amount of time that has passed since the previous Update, and you can use this to time the animation frames and animate it at the pace you need; e.g. if you want to switch to another "frame" 10 times per second, you use a float as a timer, add the deltaTime to the float every update, and when it reaches 0.1 you reset the timer and set the SpriteRenderer to have a new sprite. Since this is all done in scripting now, you have complete control over the order you do this in.

The whole framework will look something like this:

 public class AnimatedObject : MonoBehaviour
 {
     public SpriteRenderer render;
     public Sprite[] frames;
     
     private float timer = 0F;
     private const float ANIMATION_TIME = 0.1F;
 
     void Update()
     {
         timer += time.deltaTime;
         if (timer > ANIMATION_TIME)
         {
             this.setNextFrame();
         }
     }
 
     private void setNextFrame()
     {
         // your code goes here, you can set the frame with render.sprite
     }
 }


You can expand on this by setting up more than one animation, determining which animation to use, etc. This is where ScriptableObjects come in handy; you can put that sprite array from earlier in one, then make a different instance of that object for each animation you need use, so you can put your sprites in there in the order that they animate in, and then just switch out your animations based on the status of your gameobject. ScriptableObjects are essentially custom asset types, like how MonoBehaviours are custom component types. Read up on them, they're handy.

Comment
Add comment · Show 1 · 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 Endesmomo · Mar 02, 2017 at 01:43 PM 0
Share

Nice. Yes, the first thing I thought when watching tutorials was "all this to just render a sequence of frames based on the delta time?" Thanks.

avatar image
1

Answer by jjcrawley · Mar 03, 2017 at 06:23 PM

Have you tried editing the animation frames in the animation window?

I'd advise having a look at the animation window documentation: https://docs.unity3d.com/Manual/animeditor-UsingAnimationEditor.html

The basic idea would be to create one animation for your walk cycle, and another for your idle. The idle animation can be just one frame, which is him standing still, the other can be the entire walk cycle. This walk cycle clip can have the necessary walking sprites in place, in any order that you like. One thing worth mentioning is that if you want to use a different sprite sheet for a different character, you'll have to make new animation clips for each one.

Hopefully this'll lead you in the right direction.

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

183 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

2D sprite animation issue 0 Answers

Compile error with animator 1 Answer

2D Sprite animation - How do I jump to the next frame? 0 Answers

After animation completion transition to default animation 1 Answer


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