Change Materials On Button Press or Hold?

I am making a 2d game and I am using three different sprite sheets ive made as materials for animation.

alt text

I have a plane as my character and attached this script below that will cycle through the sprites using offset…


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);
}

The script works perfect…

But i need a script that would switch my idle material to a left material when i press the left key and then switch to the right material when i press the right key…
Therefore it would be an animated left and right movement
and then move back to the idle material when nothing is pressed…

can someone help me out with this please?

Here is are mods to your script that change the texture when an arrow key is pressed. You will need to set the array size of artex to 3 and drag the three textures onto the three slots in the array.

#pragma strict

var artex : Texture[];

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;

private var iTexture = 1;
private var iNewTexture = 0;
 
function Update () {
    if (artex.Length < 3) {
    	Debug.Log("Need to assign textures in the inspector");
    	return;
    }
    
    if (Input.GetKey(KeyCode.LeftArrow)) {
    	iNewTexture = 0;
    	// Insert code here to modify uvAnimationTileX for first texture
    }
    else if (Input.GetKey(KeyCode.RightArrow)) {
     	iNewTexture = 2;
     	// Insert code here to modify uvAnimationTileX for last texture
    }
    else {
    	iNewTexture = 1;
    	// Insert code here to modify uvAnimationTileX for middle texture
    }  
    
    if (iNewTexture != iTexture) {
    	iTexture = iNewTexture;
    	renderer.material.mainTexture = artex[iTexture];
    }
    
    // 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);
}