0
votes

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

1
Do you know what a state machine is? A state machine is a very simple program where the machine has a current state, and a series of inputs. Each input changes the state based only on what the input is, and what the current state is. Can you describe the state machine for your program? That is, given every possible combination of states like "is jumping", "is crouching", and so on, and given every possible combination of button inputs, what the new state is for that button combination? If you can list those state transitions you'll find this easier to implement. - Eric Lippert
@EricLippert thank you for explaining that, i have my different button inputs and i understand it, the problem was with my not understing exactly how unity works, thank you for taking the time to work with me - Konstantinos Panteli
@KonstantinosPanteli: Eric is proposing a different way of modeling your game. The benefit of the state machine approach over the flag approach you are currently using is that a state machine naturally resolves problems such as the one you proposed. Prince's answer solves your current problem more narrowly. Game Programming Patterns does a decent job explaining what that approach accomplishes, as well as explaining how far you can push it before it falls apart (answer: not very far). - Brian

1 Answers

2
votes
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