Crouch State

Hello, I am a bit new to programming (I am a 3d artist) so please excuse me if its a stupid question :stuck_out_tongue: Currently I am making an FPS and I have first person legs that are animated. Right now the legs work fine, however I am trying to make a state system. When the player presses β€œC” the player crouches and when the player presses β€œC” again the player uncrouches (a toggle function). So for the legs I wanted to create a state system so if the player presses β€œC” the Crouch State is active and vise versa. So if the player presses β€œW” while the Crouch State is active, the Crouch Walk Forward Animation plays (not just the walk animation.) Here is a piece of my code that I tried to make for the toggle state.

        //Crouch Anim
//Toggle
if (Input.GetKey(KeyCode.C)){
    Crouch=true;
    Debug.LogWarning ("Crouch is true");

}

if (Crouch==true){
if (Input.GetKey(KeyCode.C)){
        Crouch=false;
        Debug.LogWarning ("Crouch is false");
    }
         }

You probably want Input.GetKeyDown or Input.GetKeyUp. Input.GetKey will return true every frame that the key is down, not just when it changes.

Also, here’s a simpler way to write that whole block:

if (Input.GetKeyDown(KeyCode.C)){
    Crouch=!Crouch;
    Debug.LogWarning ("Crouch is " + Crouch);
}