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.left5Time.deltaTime);

changeTexture ();

}

if (Input.GetKey (KeyCode.DownArrow))

{

this.transform.Translate (-Vector3.left5Time.deltaTime);

changeTexture ();

}

if (Input.GetKey (KeyCode.LeftArrow))

{

this.transform.Rotate(-Vector3.up100Time.deltaTime);

}

if (Input.GetKey (KeyCode.RightArrow))

{

this.transform.Rotate (Vector3.up100Time.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;

}

This is from a similar question i asked a while ago

var uvAnimationTileX = 24; //Here you can place the number of columns of your sheet. 
                           //The above sheet has 24
 
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);
}

I think this is old. The best way is to use Animation Controller and Animation:

http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-overview