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 Clopay · Aug 17, 2012 at 06:52 PM · animationsprite2.5d

Sprite Animation - Problem with image progression

Hey all,

First off, thanks to everyone who's active and answering on this site. Many hours of work saved through these forums - greatly appreciated.

So what I'm trying to accomplish is a simple 2.5D sidescroller game for the sake of learning Unity/javascript more in depth. I'v been successful in most areas so far - character can move left/right, near/far, jump, etc. I've also got sprite sheets functioning properly for the most part.

Where I run into the problem is getting the sprite sheet to play ONLY a specific range of images, and NOT the entire sheet. I have 25 total images on my sprite sheet. The first image is a box describing the action of the animation, with the following 24 frames being the animation. I'd like the sprite to SKIP the first frame, and play the remaining 24. That means skip frame 1, and play 2-25.

Here's the function that handles the U/V offset to animate the sprite:

 function aniSprite (columnSize, rowSize, colFrameStart, rowFrameStart, totalFrames, framesPerSecond)
 {
     var index : int = Time.time * framesPerSecond;            //Time control FPS
     index = index % totalFrames;
     
     var size = Vector2 (1.0 / columnSize, 1.0 / rowSize);    //Establishes X/Y coordinate values for texture scale
         
     var u : int = index % columnSize;
     var v : int = index / columnSize;
     var offset = Vector2 ((u + colFrameStart) * size.x, (1.0 - size.y) - (v + rowFrameStart) * size.y);    //Establish time dependent X/Y coordinates for the texture offset
 
     renderer.material.mainTextureOffset = offset;        //Applies offset value to texture
     renderer.material.mainTextureScale     = size;            //Applies scale value to texture
 }
 

And here's an excerpt of the code I have that calls the above script to perform that animation function:

             //Idle
             if (velocity.x == 0 && velocity.z == 0)            //If player is not moving...
             {
                 renderer.material = aniMav [0];                //...changes to specified material (sprite sheet)...
                 aniPlay.aniSprite (5, 5, 1, 0, 24, 24);        //...call aniSprite script to perform function.
             }

If you notice, the aniPlay.aniSprite function is followed by (5, 5, 1, 0, 24, 24). As far as I'm concerned, this means the sprite sheet is 5 columns wide, 5 rows down, column start = 1, row start = 0, 24 total frames @ 24 FPS. But it's just not working properly. It ends up skipping every image that resides in the first column (so if my sprite sheet is 5x5, it skips 1, 6, 11, 16, and 21).

I'm wondering if this is a problem with the modular function %? Any help is greatly appreciated. Thank you for sticking around for the entire post :)

-Clopay

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 Clopay · Aug 17, 2012 at 06:52 PM 0
Share

Also - I am aware of 3rd party sprite management extensions, but I'd like to understand the rudimentary behaviors behind the scripting and how Unity handles it before I move onto those. I've done quite a bit of searching for someone with the same problem to no avail, so again, I appreciate anyone who can steer me in the right direction!

avatar image Khada · Aug 17, 2012 at 07:22 PM 0
Share

I have an answer inco$$anonymous$$g, have to await mod approval due to low karma T_T

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Clopay · Aug 17, 2012 at 10:12 PM

I've got it!

Here's the code from before:

 function aniSprite (columnSize, rowSize, colFrameStart, rowFrameStart, totalFrames, framesPerSecond)
 {
     var index : int = Time.time * framesPerSecond;        //Time control FPS
     index = index % totalFrames;
 
     var size = Vector2 (1.0 / columnSize, 1.0 / rowSize);  //Establishes X/Y coordinate values for texture scale
 
     var u : int = index % columnSize;
     var v : int = index / columnSize;
     var offset = Vector2 ((u + colFrameStart) * size.x, (1.0 - size.y) - (v + rowFrameStart) * size.y);    //Establish time dependent X/Y coordinates for the texture offset
 
     renderer.material.mainTextureOffset = offset;   //Applies offset value to texture
     renderer.material.mainTextureScale     = size;         //Applies scale value to texture
 }

And here's my updated code:

 function aniSprite (columnSize, rowSize, startFrame, endFrame, framesPerSecond)
 {
     var index : int = Time.time * framesPerSecond;            
     index = index % (endFrame - startFrame) + startFrame;           //This is new!!
     
     var size = Vector2 (1.0 / columnSize, 1.0 / rowSize);
     
     var u : int = index % columnSize;                    
     var v : int = index / columnSize;                        
     var offset = Vector2 (u * size.x, (1.0 - size.y) - (v * size.y));     //This is new!!
     
     renderer.material.mainTextureOffset = offset;            
     renderer.material.mainTextureScale     = size;                
 }

Basically, what I changed is the way the code handles the frames. Instead of having to input a starting frame and a total frame amount, you input a starting frame and an ending frame. The sprite animation loop begins on the specified start frame, plays through to the end frame, and starts back on the beginning frame. It's actually a bit less code, too.

Hope this helps anyone out there with a similar problem!

-Clopay

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 Clopay · Aug 17, 2012 at 10:10 PM 0
Share

Also, thanks to $$anonymous$$hada for jump starting my brain. $$anonymous$$uch appreciated! -Clopay

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

9 People are following this question.

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

Related Questions

Couple of Questions dealing with 2.5D 0 Answers

Custom Pivot Points in Sprite Editor 1 Answer

Animating the sortingOrder property of a Renderer 0 Answers

Sprite Animator (walker boys)- Jumping 0 Answers

2D sprite character movement 3 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