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 user5200 · Jun 15, 2012 at 03:10 AM · c#2dfightingsprite-animation

Sprite Sheet animation without fancy add-ons

Hi all,

I am trying to include some sprite animations in my 2D beat-em-up/action game. I have glanced over many of the 2d sprite management add-ons in the unity store but alas I am very poor. I do not think I will use them enough to justify purchasing them. So, despite having no experience with animation, I decided to try animating sprite sheets on my own.

It has been going well so far. I have had success with basic animations upon button click and others that cycle through the sheet over time. But I've run into a problem. My artist has decided to add some frames on the basic attack animations and it no longer seems to be working the way I intended.

Here is a working version of the current animations. This is the main character's three-hit combo string:

 if  (Input.GetButtonDown("fight"))
                 {
                     atktimer = true;
                     atkreset = 0;
                     if (attackcounter <= 3)
                         attackcounter++;
                     StartCoroutine (PlayAnimation () );
                     if (attackcounter > 3)
                         attackcounter = 0;
                 }
                 
                 if (atktimer)
                 {
                     atkreset += Time.deltaTime;
                     if (atkreset - Time.deltaTime > 2f)
                     {
                         atktimer = false;
                         attackcounter = 0;
                     }
                 }
  
   IEnumerator PlayAnimation ()
     {
                 //attack1
                 if (attackcounter == 1)
                 {
                     animating = true;
                     audio.PlayOneShot(whoosh1);
                     renderer.material.mainTextureOffset = new Vector2 (0.25f, 0.5f);
                     
                     yield return new WaitForSeconds(0.5f);
 
             
                     animating = false;
                     frameanimation = 0f;
                 }
             
             
                 //attack2
                 if (attackcounter == 2)
                 {
                     animating = true;
                     audio.PlayOneShot(whoosh2);
                     renderer.material.mainTextureOffset = new Vector2 (0.75f, 0.5f);
                     
                     
             
                     yield return new WaitForSeconds(0.5f);
 
             
                     animating = false;
                 }
                 
                 //attack3
                 if (attackcounter == 3)
                 {
                     animating = true;
                     renderer.material.mainTextureOffset = new Vector2 (0.75f, 0.25f);
     
             
                     yield return new WaitForSeconds(0.5f);
             
                     animating = false;
                 }
         
     }
 

What this does is: Every time I click the attack button, the main character cycles through 3 different images on the sprite sheet, and at the end, it returns to the first one.

What I need: Since each attack now has additional frames, instead of simply changing the renderer offsets once per click, it needs to go through 2 frames before I click the button again. This is what I have attempted to solve the problem:

In update:

    if (animating)
                 {
                     frametimer += Time.deltaTime;
                 }

And in my Playanimation method:

 //attack1
                 if (attackcounter == 1)
                 {
                     animating = true;
                     audio.PlayOneShot(whoosh1);
                 if (frametimer - Time.deltaTime > 0)
                     renderer.material.mainTextureOffset = new Vector2 (0f, 0.5f);
                 if (frametimer - Time.deltaTime > 0.25f)
                     renderer.material.mainTextureOffset = new Vector2 (0.25f, 0.5f);
                     
                     yield return new WaitForSeconds(0.5f);
 
             
                     animating = false;
                     frametimer = 0f;
                 }

As you can see, I made a new timer to keep track of the smaller frames and inserted it into the larger attack command. It should switch frames as frametimer increments itself and play each frame for 0.25 seconds each. But it does not do this. Perhaps, there is some error in my structure or logic?

Thanks for reading! I would appreciate any help or general advice on sprite animation.

Comment
Add comment · Show 2
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 user5200 · Jun 18, 2012 at 07:50 PM 0
Share

I guess everyone here uses add-ons?

avatar image whydoidoit · Jun 18, 2012 at 07:58 PM 1
Share

BTW there is a free sprite sheet manager called Othello

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Jun 18, 2012 at 08:01 PM

So you need to put a yield return new WaitForSeconds(0.25f) between your frames:

  if (attackcounter == 1)
       {
           audio.PlayOneShot(whoosh1);
           //Frame 1
           renderer.material.mainTextureOffset = new Vector2 (0f, 0.5f);
           yield return new WaitForSeconds(0.25f);
           //Frame2
           renderer.material.mainTextureOffset = new Vector2 (0.25f, 0.5f);
           yield return new WaitForSeconds(0.25f);
           //Frame3
           renderer.material.mainTextureOffset = new Vector2 (0.5f, 0.5f);
           yield return new WaitForSeconds(0.25f);
           renderer.material.mainTextureOffset = new Vector2(0f,0f); //Or whatever is the default texture
 
       }

It strikes me you would be better off always laying out your animations in blocks though so you can cycle through the texture. Write yourself a functoin to get the UV of the texture at a given "Cell" and then you just need to iterate over cells and call that function.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Character Animation Help 1 Answer

How do I flip a 3D object to look towards left or right on a 2D plane 0 Answers

2d Beat Em Up Jump Functionality 0 Answers

Co-routines, timing and framerate independence 2 Answers

Multiple Cars not working 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