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 Shizzle · Apr 10, 2011 at 06:12 AM · animation2dtexturereset

2d animation from texture

hi guys,im having trouble with animating a texture and having the ability to restart the animation,i just cant restart it. here is the code that i have for animating the texture:

var framesPerSecond = 10.0; var uIndex; var index : int; var offset : Vector2; var size : Vector2;

function Update () { index = Time.time framesPerSecond; // repeat when exhausting all frames index = index % (uvAnimationTileX uvAnimationTileY); // Size of every tile var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);

 // split into horizontal and vertical index
 var uIndex = index % uvAnimationTileX;
 var vIndex = index / uvAnimationTileX;

 // build offset
 // v coordinate is the bottom of the image in opengl so we need to invert.
 var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);

 renderer.material.SetTextureOffset ("_MainTex", offset);
 renderer.material.SetTextureScale ("_MainTex", size);

}

any ideas how i can reset the animation,so it will start from the beginning(i need to reset the animation wen i go to a new animation=a new texture) thanks in advance!8-)

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 TheZoran · Oct 16, 2012 at 05:43 PM

Hello, old question, but nobody answered, so i'll give a try. First all, I don't use the code of the script like u did(wiki), i made my own after playing around in the inspector values and reduced it to the most important things.

Sorry I use C# but in JS you can just put it into the start function or a normal function and call it once i think, because the for-loops never end.

You have to set variables in the inspector, be sure that AnimationCyclePerSecond isn't 0

 using UnityEngine;
 using System.Collections;
 
 public class TextureAnimationScript : MonoBehaviour
 {
     public int TilesX;
     public int TilesY;
     public float AnimationCyclesPerSecond;
 
     void Start ()
     {
         renderer.material.mainTextureScale = new Vector2(1.0f / TilesX, 1.0f / TilesY);
         StartCoroutine(ChangeOffSet());
     }
     
     IEnumerator ChangeOffSet ()
     {
         for(int i = TilesY - 1; i > -1; i--)
         {
             for(int j = 0; j < TilesX; j++)
             {
                 renderer.material.mainTextureOffset = new Vector2(1.0f / TilesX * j, 1.0f / TilesY * i);
                 yield return new WaitForSeconds(1.0f / (TilesX * TilesY * AnimationCyclesPerSecond));
                 if(i == 0 && j == TilesX - 1)
                 {
                     i = TilesY;
                 }
             }
         }
     }
 }
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
avatar image
0

Answer by MadDave · Oct 16, 2012 at 03:13 PM

You need to store the time when the animation was started and then calculate

 index = Time.time - AnimationStartTime * framesPerSecond;

Resetting the Animation is then as easy as setting AnimationStartTime to the current time.

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

2 People are following this question.

avatar image avatar image

Related Questions

reset 2d animation(sprite sheet texture) 0 Answers

2D eyes on 3D character? 2 Answers

Most efficient way to do 2D animation 2 Answers

How change sprite animation texture?? 2 Answers

2d Sprite Sheet on characters ruined in unity 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