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