play animation on key press help

i am currently using the following script to control my character. At the moment the ‘crouch’ animation is played when the ‘c’ button is held down, i need to change it so the animation plays when the ‘c’ button is pressed instead of held down.

does anyone know how to do this?

//animation["CROUCH"].layer=1;

function Update () 
{ 
   if (Input.GetAxis("Vertical") > 0.2){ 
       animation.CrossFade ("WALK");
	   
   }else if(Input.GetAxis("Vertical") < -0.2){ 
       animation.CrossFade ("REVERSE");
	   // Play animation backwards when running backwards 
      animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical")); 
	   
   }else{ 
      animation.CrossFade ("IDLE"); 
   } 
   
    if(Input.GetKey("c")){ 
       animation.CrossFade ("CROUCH");
}
}

any help would be great

There’s an Input.GetKeyDown function that works like GetKey, but only registers the keystroke on the first frame it happens.

i replaced ‘GetKey’ with ‘GetKeyDown’ as you said, but now the ‘crouch’ animation doesnt play at all.

is there something im doing wrong?

Yes, sorry - I’ve just spotted what is wrong with that. The CrossFade function needs to be called each frame, so the GetKeyDown call needs a bit of extra work. If you set a boolean variable when the key is pressed, you can test for it as long as you need to run the crouch animation:-

var crouching: Boolean;  // Declared at top of script.
   ...

if(Input.GetKey("c")){ 
   crouching = true;
}

if (crouching) {
       animation.CrossFade ("CROUCH"); 
}

hi,

i have altered the script to include what you have said so the script looks like this -

//animation["CROUCH"].layer=1;
var crouching: Boolean;

function Update () 
{ 
   if (Input.GetAxis("Vertical") > 0.2){ 
       animation.CrossFade ("WALK");
	   
   }else if(Input.GetAxis("Vertical") < -0.2){ 
       animation.CrossFade ("REVERSE");
	   // Play animation backwards when running backwards 
      animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical")); 
	   
   }else{ 
      animation.CrossFade ("IDLE"); 
   } 
   
    if(Input.GetKey("c")){ 
   crouching = true; 
} 

if (crouching) { 
       animation.CrossFade ("CROUCH"); 
} 
}

but now it is coming up with the error which i dont understand -

Try “boolean”, lowercase “b”.

that got rid of the error message, but now the ‘crouch’ animation plays every 5 seconds without having to press the button, and the ‘walk’ animation doesnt work.

this is doing my head in.

Seems like you need to set the “crouching” variable to false at some point. If not, once it becomes true, it will remain true forever.

in here:
if (crouching) {
animation.CrossFade (“CROUCH”);
}

define variable and check if it become true again and set it to false after once script ran, and turn the value back when u walk…

so for next chouch u can use that again… so till you don’t walk the variable have false value(after 1 shot runing) and don’t allow your char to do chouching again

could someone post some code which would fix this? im very new to javascript and not to sure what im doing.

In your code, I don’t see anywhere that you put crouching back to false.

You initialize it as false, then, when the user hits “c” you set it to true, but then it never goes back to false.

So you should set it to false at some point…

Or, do not use a crouching variable and go with the deadManN suggestion.

Replace this:

 if(Input.GetKey("c")){ 
   crouching = true; 
}

with what deadManN suggested:

if (crouching) { 
animation.CrossFade ("CROUCH"); 
}

i think ive done what you said but now he doesnt crouch, can you see what im doing wrong?

//animation["CROUCH"].layer=1;
var crouching: boolean;

function Update () 
{ 
   if (Input.GetAxis("Vertical") > 0.2){ 
       animation.CrossFade ("WALK");
	   
   }else if(Input.GetAxis("Vertical") < -0.2){ 
       animation.CrossFade ("REVERSE");
	   // Play animation backwards when running backwards 
      animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical")); 
	   
   }else{ 
      animation.CrossFade ("IDLE"); 
   } 
   
if (crouching) { 
       animation.CrossFade ("CROUCH"); 
} 
}

My bad.

Use this instead:

if(Input.GetKey("c")){ 
   animation.CrossFade ("CROUCH"); 
}

thats taken it back to my origional problem, the crouch animation is played when the key is held down. I need it to play in full when the key is pressed.

then you need to set your CROUCH animation to WrapMode.Once and use something like this:

if(Input.GetKey("c"))
{ 
       animation.CrossFade ("CROUCH"); 

       while(animation.isPlaying)
            yield;
}

Which will cause problems in a Update function though, because of the yield (or maybe not in javascript? I dont really use javascript)

You should probably use the layer property of the animation state.

You should set the crouching animation in a bigger layer than the idle.

Therefore, while the crouching animation is playing, the idle will not play. But as soon as the crouching stops, idle will start.

Check the documentarion regarding Layer in the AnimationState.

i am already using the layer function -

//animation["CROUCH"].layer=1;
var crouching: boolean;

function Update () 
{ 
   if (Input.GetAxis("Vertical") > 0.2){ 
       animation.CrossFade ("WALK");
	   
   }else if(Input.GetAxis("Vertical") < -0.2){ 
       animation.CrossFade ("REVERSE");
	   // Play animation backwards when running backwards 
      animation["REVERSE"].speed = Mathf.Sign(Input.GetAxis("Vertical")); 
	   
   }else{ 
      animation.CrossFade ("IDLE"); 
   } 
   
if(Input.GetKey("c")){ 
   animation.CrossFade ("CROUCH"); 
} 
}

any other ideas?

In your code, the layer sentence is commented out.

Also, you’re using 1 (one) as a value for the crouch layer. I would suggest using something higher, like 2 or 3. Since 1 might be the default (not sure about this, but just to be safe).

And if you want to be extra careful, also set the layers of the walk and reverse animation to a lower value, and the idle to an even lower value.

I would use:
idle at layer -5
walk / reverse at layer -1
crouch at layer 2

THANKS, THATS WORKED PERFECTLY!