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 POLYGAMe · Jul 02, 2011 at 02:15 AM · animationspritestexturesloopinganimated-texture

How to stop animated texture looping in Unity Indie?

Hi guys,

I am using this script to animated my explosion "sprite" but I can't stop it from looping. How would I do this? I commented out the repeat part of the script but it did nothing....

  var uvAnimationTileX = 4; //Here you can place the number of columns of your sheet. 
                            //The above sheet has 4
 
 var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet. 
                           //The above sheet has 1
 var framesPerSecond = 10.0;
 
 function Update ()
 {
     // Calculate index
     var index : int = 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);
 }
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 POLYGAMe · Jul 02, 2011 at 02:16 AM 0
Share

Hmmm, not sure what happened to the formatting up there!

avatar image testure · Jul 02, 2011 at 02:21 AM 0
Share

fixed it for you.

avatar image POLYGAMe · Jul 02, 2011 at 02:40 AM 0
Share

Cheers ;-)

avatar image POLYGAMe · Jul 02, 2011 at 10:31 AM 0
Share

Sorry for the bump, but anyone? Stuck here and would rather not use bools and if statements for something that's probably pretty simple. LOL.

2 Replies

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

Answer by Waz · Jul 02, 2011 at 11:33 AM

This code doesn't just loop, it also starts running from the beginning of the game. Here's a rewrite:

  var uvAnimationTileX = 4; //Here you can place the number of columns of your sheet. 
                            //The above sheet has 4
 
 var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet. 
                           //The above sheet has 1
 var framesPerSecond = 10.0;
 private var t = 0.0;

 function Update ()
 {
     // Calculate index
     t += Time.deltaTime;
     var index : int = t * framesPerSecond;
     
     // stop when exhausting all frames
     if (index >= uvAnimationTileX * uvAnimationTileY)
         return;
     
     // 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);
 }

Note that while I wrote return, probably what you actually want is to Destroy the gameObject, since the explosion will stay at the last frame unless you reset t to 0.

Comment
Add comment · Show 3 · 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 POLYGAMe · Jul 02, 2011 at 08:40 PM 0
Share

Hi, thanks for that! Yeah, I will be deleting the explosion... I should have been more clear, I also want to use this for my main ship's animation etc, so in some cases I don't need to destroy the object. Thanks again!!!

avatar image Waz · Jul 02, 2011 at 09:24 PM 0
Share

Both @synapsemassage and I mean that the explosion should be it's own prefab which you Instantiate when needed, and it should Destroy itself when done. Whether and when you Destroy the thing "hit" by this explosion of course depends on your game. (ps. If the question is answered, mark it so)

avatar image POLYGAMe · Jul 02, 2011 at 10:20 PM 0
Share

Good points, thanks again, mate :)

avatar image
1

Answer by synapsemassage · Jul 02, 2011 at 11:23 AM

Add the script when you need it. Destroy gameobject when you don't need it anymore and respawn it without the script component. (Keep an eye on memory leaks: search for "destroy" and "memory leak", "instanciate", "garbage collector" ). But why don't you want to use booleans and if-loops?

http://unity3d.com/support/documentation/ScriptReference/GameObject.AddComponent.html

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 POLYGAMe · Jul 02, 2011 at 08:42 PM 0
Share

Hi, I can destroy the object for the explosion but I also need to use this for other characters that aren't being destroyed... like my main player ship. I should have been more clear. LOL. I just thought that using bools and if statements based on timers might not be the best or most accurate way to do this and being a code noob, I'd like to get into the habit of doing things properly... though if that IS the proper way I'll do that.... just thought there might be something simpler ;-)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to convert multiple images to a single composite image for animations? 0 Answers

Flipping animation in 2D sprites 2 Answers

Deformation Maps Possible? 0 Answers

Should I make an animator for each weapon or a master animator with all the weapons in it and call them respectively? 0 Answers

Changing texture depending on angle to another player 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