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 KG3 · Jul 16, 2013 at 01:01 PM · animation2dsprite

Animataion of a Sprite v2

Hey Unity Community, I am trying to animate this sprite, in this scene. I have asked this question before, and some cool people helped me to get started.

For some reason nothing is happening when I play the game.

This is the sprite sheet: alt text

To make the sprite move, it has to increase the OffsetX from (0,0.33,0.66) and decrease the offsetY from (0.66,0.33,0). When the coordinates get to X = 0.66, and the Y is at zero, there is nothing there, so I just reset the coordinates back at the first sprite.

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class SpriteSheet_Asuka : MonoBehaviour
 {
     // Use this for initialization
     //OffsetX and offsetY to set where it the sprite starts.
     public float offsetX = 0f;
     public float offsetY = 0.66f;
     public float tilingX = 0.3f;
     public float tilintY = 0.3f;
 
 
     void Start()
     {
 
         animate();
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         renderer.material.mainTextureOffset = new Vector2(offsetX, offsetY);
         renderer.material.mainTextureScale = new Vector2(tilingX, tilintY);
 
     }
 
     IEnumerable animate()
     {
        int currentFrames = 0;
         int totalFrames = 7;
         while (totalFrames > currentFrames)
         {
             renderer.material.mainTextureOffset = new Vector2(offsetX, offsetY);
 
             if (offsetX > 0.66f)
             {
                 offsetX = 0f;
                 offsetY -= 0.33f;
             }
             else if (offsetY == 0f && offsetX == 0.66f)
             {
                 offsetX = 0f;
                 offsetY = 0.66f;
             }
             else
             {
                 offsetX += 0.33f;
                 currentFrames++;
             }
 
 
             if (currentFrames == 7)
             {
                 currentFrames = 0;
             }
 
         }
         yield return new WaitForSeconds(1);
 
     }
 
 
 }
 
 


asuka.png (12.3 kB)
Comment
Add comment · Show 4
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 markedagain · Jul 16, 2013 at 01:15 PM 0
Share

hey $$anonymous$$G3,

this is already looking much better then your version one, but still needs a bit of work.

the scale function should only be called once, so moved that into your start function, and u dont need to update the offset in the the update cause you already doing it in the animate function.

also im a bit confused why yoiu starting off with an already offset, but you not starting with currentframe = 7.

one last thing, is this a idle animation?are u hooping to loop the animation or its a 1 shot animation where u do all the frames then stop ?

avatar image KG3 · Jul 16, 2013 at 02:54 PM 0
Share

Yes, that is exactly what I am trying to do. this is a idle animation, where I do all the frames then stop, in a loop, when the character is standing still.

avatar image markedagain · Jul 16, 2013 at 07:42 PM 0
Share

ok as robertbu suggested u need to call your animation using startCoroutine, after this u will ahve to come up with a better way to handle the animation none stop. i would recommend putting some sort of bool IsAnimating, and have it set to true when u are in your while loop, and then in your update functino have a simepl if (IsAnimate == false) then animate away.

but again keep in $$anonymous$$d, im not sure why you have a starting offset and your starting frame is set to 0

avatar image KG3 · Jul 22, 2013 at 12:18 AM 0
Share

The offset is the sprite sheet's coordinates, so at the X = 0, and y = 0.66 the offset is the top of the sprite sheet.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jul 16, 2013 at 02:53 PM

You need to use StartCoroutine() in C#. Line 17 should be:

 StartCoroutine(animate());

Coroutines need to be of type 'IEnumerator' not 'IEnumerable', so line 31 should be:

 IEnumerator animate()

Your 'yield' needs to be inside your while() loop. So move it from line 62 to line 60. If you don't do this, you will hang Unity.

I think there are some issues with your step logic for each frame, but these three changes will get you up and running.

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

16 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

Related Questions

SpriteManager 2 1 Answer

Sprite animation 2 Answers

How to make not smooth animation? 0 Answers

How can i start a 2D animation via script 1 Answer

How do I stop sprite animation on last frame? 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