|
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; }
(comments are locked)
|

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..