Changing between Prone, Crouch and Stand with one key.

Hi,

I’m having some problems with making my character switch between prone, crouch and stand stances. I’m trying to copy the stance system from MGS:V where all the changes are made by either pressing or holding F.

Currently my code’s problem is that I can’t switch back to stand mode after going either crouch or prone. I believe this is due to the button presses function changes according to a bool that it’s supposed to change.

The code is supposed to work like this:

F press while standing: crouch

F press while crouch: stand

F press while prone: crouch

F hold while stand: prone

F hold while crouch: prone

F hold while prone: stand

The cLongPress bool is supposed to count the time the key is pressed and it seems to work fine and the reason i used keyUp for pressing C (not holding) is so that the timer actually has a change to go to to the cRequeredPressTime


public float cPressTimeRequered = 0.5f;
public float cPressTime = 0;
public bool cLongPress = false;
public bool isGrounded;
public bool isStanding;
public bool isCrouched = false;
public bool isProne = false;

// Use this for initialization
void Start()
{
    isStanding = true;
}

// Update is called once per frame
void Update()
{
    if (Input.GetKey(KeyCode.C))
    {
        cPressTime += Time.deltaTime;
    }
    if (Input.GetKeyUp(KeyCode.C))
    {
        cPressTime = 0;
    }

    //To Stand
    if (Input.GetKeyUp(KeyCode.C) && cLongPress == false && isCrouched)
    {
        isStanding = true;
        isCrouched = false;
        isProne = false;
    }
    if (Input.GetKey(KeyCode.C) && cLongPress == true && isProne)
    {
        isStanding = true;
        isCrouched = false;
        isProne = false;
    }

    //To Crouch
    if (Input.GetKeyUp(KeyCode.C) && cLongPress == false && isStanding)
    {
        isStanding = false;
        isCrouched = true;
        isProne = false;
    }
    if (Input.GetKeyUp(KeyCode.C) && cLongPress == false && isProne)
    {
        isStanding = false;
        isCrouched = true;
        isProne = false;
    }

    //To Prone
    if (Input.GetKey(KeyCode.C) && cLongPress == true && isStanding)
    {
        isStanding = false;
        isCrouched = false;
        isProne = true;
    }
    if (Input.GetKey(KeyCode.C) && cLongPress == true && isCrouched)
    {
        isStanding = false;
        isCrouched = false;
        isProne = true;
    }
}

The only solution that I’ve been able to find was to use animations to change the stance but I would prefer to make the script somewhat independent of the animator.,

Good day.

First, you need to difference between:

GetKeyDown (is true only the frame user has commenced to press the key)

GetKey (is true during all frames the commenced and finished with the key pressed)

So you can do something like

if (GetKeyDown)
{
bla bla
 if (GetKey)
  {
  }
}

ind inside that, make if and else if sentences to know the state (proud, stand…) to decide what to do.

I got it working myself.

I made it so that instead of the prone stance being directly triggered by pressing C in which case the signal is continuous it is triggered by a system that only sends one signal per one hold.

Here is my code:

public bool cLongPress = false;
public bool isStanding = true;
public bool isCrouched = false;
public bool isProne = false;

public float cPressTimeRequered = 0.5f;
public float cPressTimeReset = 0;
public float cPressTime = 0;
public bool continusPressCheck = false;

// Use this for initialization
void Start()
{
    isStanding = true;
    isCrouched = false;
    isProne = false;
}

// Update is called once per frame
void Update()
{
    //Stance trigger timer
    if (Input.GetKey(KeyCode.C) && continusPressCheck == false)
    {
        cPressTimeReset += Time.deltaTime;
    }
    if (Input.GetKeyUp(KeyCode.C) || continusPressCheck==true)
    {
        cPressTimeReset = 0;
    }
    if (Input.GetKey(KeyCode.C))
    {
        cPressTime += Time.deltaTime;
    }
    if (cPressTimeReset >= cPressTimeRequered && continusPressCheck == false)
    {
        continusPressCheck = true;
        cLongPress = true;
    }
    else
    {
        cLongPress = false;
    }
    if (Input.GetKeyUp(KeyCode.C))
    {
        continusPressCheck = false;
    }

    //Stances
    if (cLongPress)
    {
        isProne = !isProne;
        isCrouched = false;
    }
    if (Input.GetKeyUp(KeyCode.C) && cPressTime<cPressTimeRequered)
    {
        isCrouched = !isCrouched;
        isProne = false;
    }
    if(isCrouched == false && isProne == false)
    {
        isStanding = true;
    }
    else
    {
        isStanding = false;
    }
}

//Update is called once per frame ends
void LateUpdate()
{
    if (Input.GetKeyUp(KeyCode.C))
    {
        cPressTime = 0;
    }
}