make player crouch

i am using the following script to controll the animations assidned to my player.

what i am wanting to do is add something to it which enables the ‘crouch’ animations when a key is pressed.

does anyone know how i can do this?

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

ive tried the following but it doesnt seem to work -

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 if(Input.GetKeyDown("c")){ 
       animation.CrossFade ("CROUCH");
	 
	   
   }else{ 
      animation.CrossFade ("IDLE"); 
   } 
}

any ideas?

does nobody know how to do this?

I don’t know for sure but shouldn’t the crouch animation be on a higher layer than the other animations? Look in the manual on how to do it because I am on a school computer and can’t access it at the moment… good luck!

-Blayke

ok, i have put the crouch animation on the highest layer and it plays perfectly when i press the ‘c’ button.

however, i want to be able to walk using the ‘w’ key and crouch at the same time, as it is now i have to let go of the walk key and then press the crouch key.

here is the script -

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 if(Input.GetKeyDown("c")){ 
       animation.CrossFade ("CROUCH");
	 
	   
   }else{ 
      animation.CrossFade ("IDLE"); 
   } 
}

I would change GetKeyDown to just GetKey - GetKeyDown only returns true on the frame that the button was pressed, but returns false on all frames after that until the button is pressed again. GetKey will (I think) return true while the button is down, and false when the button is up.

As it is now, your script fades into the crouch animation for a single frame, and then immediately back int the idle animation.

i still need to take my finger off the walk button and then press the crouch button.

i want to be able to press the crouch button whilst holding down the walk button.

In that case, you’d have to change the else if(Input.GetKey(“c”)){ line to a pure ‘if’ statement that starts it’s own chain.

The ‘else’ statement that starts the idle animation would also probably need to change to it’s own pure ‘if’ statement, something along the lines of:

if (!Input.GetKey(“c”) ((Input.GetAxis(“Vertical”) <= 0.2) || (Input.GetAxis(“Vertical”) >= -0.2))){
animation.CrossFade(“IDLE”)
}

My tests yesterday show that only one key press at a time will register. The workaround is to store the keypress as a boolean and make a conditional that is both keys have been booleaned to true then do both animations at the same time. The next trick is unregistering the bool according to the key not being pressed and whether you want the crouch to take over, the idle or walk when the yadda yadda key is unpressed versus the bing bang key being pressed., etc…etc…etc. Other than that kind of workaround you are stuck with the one key being registered at a time paradigm.

HTH

One key registering at a time sounds to me more like a limitation of your controller/keyboard, not necessarily of Unity. I have script that checks for GetKey(whatever) with a bunch of different things, and I can certainly check if two keys are held down simultaneously.

That is what I said. Make checks. Post your script…don’t just tease us with it’s possible existence:)

as im fairly new to scripting, can anyone help with a script which can do this?

If you want to be able to walk and crouch at the same time, just get rid of the “else” in the

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

It’s the same concept as if you want to walk diagonally in a direction using a combination of directions. for example.

-Pseudocode-

if(left is press){go left}

if(right is press){go right}

if(up is press){go up}

if(down is press){go down}

  • end pseudocode

so if you wanted to go up and left at the same time, the if statements all get tested, not one or the other.

thanks thats worked perfectly!