Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with using a boolean in Unity3D?

I am using a character controller and am trying to prevent the player from being able to dash forward whilst they are currently in a crouched position. I use booleans everywhere so I cannot figure out why the boolean for this doesn’t work.

bool canDash = true;
bool isCrouching = false;
bool isCeilingAbove; // this uses a Physics.CheckSphere and GameObject

void Update()
{
    dashCooldown -= Time.deltaTime;

    if (isCeilingAbove)
    {
        isCrouching = false;
    }
    else
    {
        cc.height = Mathf.Lerp(startHeight, crouchHeight, currentCrouchAmount);
        cc.center = Vector3.Lerp(startCenter, crouchCenter, currentCrouchAmount);

        float crouchTargetValue = Input.GetKey(KeyCode.LeftControl) ? 1f : 0f;

        currentCrouchAmount = Mathf.SmoothDamp(currentCrouchAmount, crouchTargetValue, ref currentCrouchVelocity, 0.25f);

        isCrouching = !isCrouching;
    }

    if (!isCrouching)
    {
        canDash = true;
    }

    if (canDash)
    {
        if (Input.GetKey(KeyCode.V))
        {
            if (dashCooldown <= 0)
            {
                StartCoroutine(Dash());
            }
        }
    }
}

I don’t have any booleans inside my IEnumerator itself and these are all the references I have to both the isCrouching & canDash booleans.

If I use basic crouch code like the one below, my canDash bool works. So I know that my code is fine, it’s just I don’t know how to implement that bool in my real code above.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if (Input.GetKey(KeyCode.LeftControl))
{
    cc.height = crouchHeight;
    cc.center = crouchCenter;
    isCrouching = true;
    canDash = false;
}
else
{
    cc.height = startHeight;
    cc.center = startCenter;
    isCrouching = false;
    canDash = true;
}

>Solution :

isCrouching = !isCrouching;

alternates the state each frame forth and back. you should probably rather simply use

isCrouching = Input.GetKey(KeyCode.LeftControl);

And actually canDash seems also quite redundant, why not simply check for if(!isCrouching)

I would probably rather have something like e.g.

void Update()
{
    dashCooldown -= Time.deltaTime;

    // so if there is a ceiling and you are currently couching than you stay crouching
    // otherwise you directly take the key pressed state
    isCrouching = isCeilingAbove && isCrouching ? true : Input.GetKey(KeyCode.LeftControl);

    var crouchTargetValue = isCrouching ? 1f : 0f;

    currentCrouchAmount = Mathf.SmoothDamp(currentCrouchAmount, crouchTargetValue, ref currentCrouchVelocity, 0.25f);

    cc.height = Mathf.Lerp(startHeight, crouchHeight, currentCrouchAmount);
    cc.center = Vector3.Lerp(startCenter, crouchCenter, currentCrouchAmount);

    // I would even limit the dash to only work if fully standing
    if (Mathf.Approximately(currentCrouchAmount, 0f) && dashCooldown <= 0 && Input.GetKey(KeyCode.V))
    {
        StartCoroutine(Dash());
    }
}

might then introduce another flag isDashing which is true as long as your Dash coroutine is running and prevent crouching until it is finished. Then you wouldn’t even need the dashCooldown but could simply check for if(!isDashing)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading