how to reverse the key press

my character movements are now working but i want to the exchange the animation of the key press “a” and “d” to each other. the “a” has a left movement while the “d” has the right movement…

I want to exchange their animations while im pressing “s”… :slight_smile:

here is my code…

var speed = 10.0;
var rotateSpeed= 1.0;

function Update ()
{

var forward = transform.TransformDirection(Vector3.forward);
var controller : CharacterController =
GetComponent(CharacterController);

//forward movement
var curSpeed = speed * Input.GetAxis (“Vertical”);
controller.SimpleMove(forward * curSpeed);

if(Input.GetKey(“w”))
animation.CrossFade(“run”);
animation[“run”].wrapMode= WrapMode.Loop;

if(Input.GetKeyUp (“w”))
animation.CrossFade (“idle”);
animation[“idle”].wrapMode = WrapMode.Once;

//left movement
transform.Rotate(0, Input.GetAxis (“Horizontal”) * rotateSpeed ,0);

if (Input.GetKey(“a”) )
animation.CrossFade (“left”);
animation[“left”].wrapMode = WrapMode.Loop;

if(Input.GetKeyUp (“a”))
animation.CrossFade (“idle”);
animation[“idle”].wrapMode = WrapMode.Once;

//right movement
if (Input.GetKey(“d”) )
animation.CrossFade (“right”);
animation[“right”].wrapMode = WrapMode.Loop;

if(Input.GetKeyUp (“d”))
animation.CrossFade (“idle”);
animation[“idle”].wrapMode = WrapMode.Once;

//reverse movement
if (Input.GetKey(“s”))
animation.CrossFade(“reverse”);
animation[“reverse”].wrapMode = WrapMode.Loop;

if(Input.GetKeyUp(“s”))
animation.CrossFade(“idle”);
animation[“idle”].wrapMode = WrapMode.Once;

//this the code that im trying to fix for exchanging animation
while (Input.getKey(“s”)){
Input.GetKey(“a”);
animation.CrossFade(“right”);
}

First - refer to the answers in http://forum.unity3d.com/viewtopic.php?t=63701&highlight=

All your “if” statements need amending e.g.

if(Input.GetKey("w"))
animation.CrossFade("run");
animation["run"].wrapMode= WrapMode.Loop;

Needs to be …

if(Input.GetKey("w"))
{
   animation.CrossFade("run");
   animation["run"].wrapMode= WrapMode.Loop;
}

Second, create two string variables that hold the left and right keys (e.g. keyLeft=“a”).

Everywhere it says GetKey(“a”) replace it with GetKey(keyLeft) (similar for the right key).

In the code where you hold down the “s” key switch the values of the leftKey and rightKey variables, and reinstate them when you lift the “s” key.

Third, read some books on programming. Don’t take this the wrong way - I’m not being aggressive here but I believe you will learn far quicker through research and trying to solve problems yourself then having a full solution written out for you.

thank you for the advice sir… I also realized that. :smile:
im not good in programming but ill do my best to be good one… :slight_smile: