x


Sprite animate on start ?

hi Guys i created a script which makes my sprite move, what i did is i created an texture array which hold my sprite images i know i should use a sprite sheet but i dont like it that way, anyways what i want to do is when Unity runs i want the sprite to run automatically. I want my array to cycle through the number of elements within the array and once it has reached to the end i want it to restart. I have created a function at the bottom and that's where i want the code written in and then i want to call it on the start function here is my script:

the array that hold the images is var idleFrames : Texture [];

var MoveSpeed : float =  5;

var CurrentFrame: int =0;


var altTerryTexture : Texture[];
var TerryIdleTexture: Texture [];

var idleFrames : Texture [];



function Start (){

     StartSpriteAnime();
}



function Update () {


     if (Input.GetKey(KeyCode.A)){

        transform.Translate(Vector3(MoveSpeed * Time.deltaTime, 0 ,0));
        renderer.material.mainTexture = altTerryTexture[CurrentFrame];

     }

     if (Input.GetKey(KeyCode.D)){

        transform.Translate(Vector3(-MoveSpeed * Time.deltaTime , 0, 0));
        renderer.material.mainTexture = TerryIdleTexture[CurrentFrame]; 

     }

}

function StartSpriteAnime(){
} 

thanks in advance :)

alt text

more ▼

asked Mar 02 '11 at 03:45 PM

MC HALO gravatar image

MC HALO
878 74 94 111

We need to see more of your sprite system.

Mar 02 '11 at 09:45 PM Peter G

What do you mean ? do you want to see how the images are set and saved ? :)

Mar 03 '11 at 12:59 AM MC HALO
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You might be better off just including it in the update rather than making it a coroutine, not sure. If you include a "var spriteDelay:int;" for the time( in seconds) between sprite frames, you could do something similar to this (Javascript examples!):

call it like so:

var repeatTime : float = spriteDelay *idleFrames.length;
InvokeRepeating("StartSpriteAnime",0, repeatTime);

That should properly loop the sprite animation.

 function StartSpriteAnime(){

        for (tex in idleFrames){    

            renderer.material.mainTexture = tex;        
            yield.WaitForSeconds(spriteDelay);

        }
    }

Alternatively you could try using the mathf.repeat function:

function StartSpriteAnime(){

    while (playSpriteAnimation){    

        var timeOffset : float = Time.time / spriteDelay;
        var currentSprite : int = Mathf.Repeat(timeOffset, idleFrames.length-1);
        renderer.material.mainTexture = idleFrames[currentSprite];      
        yield;      
    }       

}

This second one involves a boolean "playSpriteAnimation" that you could set to false if needed to break the loop.

more ▼

answered Mar 03 '11 at 04:08 AM

Alec Slayden gravatar image

Alec Slayden
1.2k 1 4 15

what is tex is that texture would i need to create a var for that

Mar 03 '11 at 04:17 AM MC HALO

Hey dude your 1st one works but the problem now is it does not loop it plays once and that is it

Mar 03 '11 at 04:25 AM MC HALO

tex will be created automatically, you don't need to create a var for it. As for the 1st one working only once, looks like bad code, I don't think a function can call itself without errors, try the second one.

Mar 03 '11 at 05:02 AM Alec Slayden

Where do i put the code:

var repeatTime : float = spriteDelay *idleFrames.length; InvokeRepeating("StartSpriteAnime",0, repeatTime);

under what line

sorry about this i am new to loops i hate them lol

Mar 03 '11 at 02:39 PM MC HALO

Dude you are a life saver :) it works the second one :) you are a hero lol thank you so much :)

Mar 03 '11 at 02:54 PM MC HALO
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3717
x294
x212
x58
x58

asked: Mar 02 '11 at 03:45 PM

Seen: 1308 times

Last Updated: Mar 03 '11 at 04:04 AM