- Home /
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);
}
Hmmm, not sure what happened to the formatting up there!
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.
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.
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!!!
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)
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
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
![](https://koobas.hobune.stream/wayback/20220612131200im_/https://answers.unity.com/themes/thub/images/avi.jpg)