x


Animating 2D sprite upon movement script help.

Hi, I can't seem to get my script working, which is meant to animate my sprite upon movement. Can anybody tell me what is wrong with this script? I have all of the images in the right place, it just seems to animate even when not moving.

Here is the script:

public var frameSpeed:float=0.05;

private var nextFrame:float=0;

public var sprites:Texture2D [];

private var counter:int=0;

function Update ()

{

if (Input.GetKey(KeyCode.UpArrow))

{

this.transform.Translate(Vector3.left*5*Time.deltaTime);

changeTexture ();

}

if (Input.GetKey (KeyCode.DownArrow))

{

this.transform.Translate (-Vector3.left*5*Time.deltaTime);

changeTexture ();

}

if (Input.GetKey (KeyCode.LeftArrow))

{

this.transform.Rotate(-Vector3.up*100*Time.deltaTime);

}

if (Input.GetKey (KeyCode.RightArrow))

{

this.transform.Rotate (Vector3.up*100*Time.deltaTime);

}

this.renderer.material.SetTexture ("_MainTex", sprites[counter]);

counter++;

if (counter==sprites.Length) counter=0;

}

function changeTexture ()

{

this.renderer.material.SetTexture("_MainTex", sprites[counter]);

if (Time.time>nextFrame)

{

counter++;

nextFrame=Time.time+frameSpeed;

}

if (counter==sprites.Length) counter=0;

}

more ▼

asked Dec 10 '11 at 05:32 PM

SWhite92 gravatar image

SWhite92
1 1 1 1

Almost all of the code will only be executed on key input, the only thing that looks a little suspicious to me are the lines (In Update):

this.renderer.material.SetTexture ("_MainTex", sprites[counter]);

counter++;

if (counter==sprites.Length) counter=0;

seeing as they are being run each frame, might be totally off though..

Dec 10 '11 at 05:55 PM merry_christmas
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
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:

x3330
x1367
x1037
x215
x104

asked: Dec 10 '11 at 05:32 PM

Seen: 1252 times

Last Updated: Dec 10 '11 at 05:55 PM