简体   繁体   English

如何同时检查两个bool Unity,C#

[英]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. 我的问题:当角色蹲下时跳跃时,击打盒会在空中飞行时变小,我想通过检查跳跃为真并且用户按下按钮“ 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: 但是由于我的ifs在Update方法内部,所以我无法使用,因为我创建了一个无限循环。我尝试使用像下面这样的双重检查:

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. 我的想法是使用while循环,但我不能这样做,我如何才能创建可以在我的跳跃为真时检查的东西,那么我就不能蹲下。 (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 在中间的陈述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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