简体   繁体   中英

How to check for two bool at the same time Unity, C#

I have a character that can jump and also crouch with this:

    if ((Input.GetButtonDown("Jump")))
    {
        jump = true;
        animator.SetBool("IsJumping", true);
    }
    if (Input.GetButtonDown("Crouch"))
    {  
        crouch = true;
        runSpeed = 0f;

    }else if (Input.GetButtonUp("Crouch"))
    {
        crouch = false;
        runSpeed = 20f;
    }

My character has a fairly large hitbox and when he crouches the hitbox gets considerably smaller. My issue: While the character is jumping if they crouch the hitbox will get smaller while they are in the air, i want to avoid that by checking that while jump is true and the user presses the button "crouch" the character won't crouch. But because my ifs are inside the Update method i can't use while because i create an infinite loop.I tried using a double check like:

if jump and crouch then no crouch

if no jump and crouch then crouch

But that doesnt work either. My idea was to use while loops but i can't so how can i create something that can check that while my jump is true then i can't crouch. (i have a function that does ground check so my jump will turn to false as soon as the character hits the floor)

thank you

update-this is not a case of & or && , thank you

if ((Input.GetButtonDown("Jump")))
    {
        jump = true;

        animator.SetBool("IsJumping", true);
    }
    if (Input.GetButtonDown("Crouch"))
    {  
         if(!animator.GetBool("IsJumping")){
        crouch = true;
        runSpeed = 0f;
        }

    }else if (Input.GetButtonUp("Crouch"))
    {
        crouch = false;
        runSpeed = 20f;
    }

note in particular, this

        if(!animator.GetBool("IsJumping")){
        crouch = true;
        runSpeed = 0f;
        }

in the middle statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM