Home > Enterprise >  why my variable wont change to what it should be in unity
why my variable wont change to what it should be in unity

Time:07-05

so when im running the "Running" code, my moveSpeed variable wont change to 4. this is happening when im adding the "Crouching" code. when i deleted or commented the crouching code, my running code works well

so here is my code

    //Running
    if (Input.GetKey(KeyCode.LeftShift))
    {
        animator.SetBool("isRunning", true);
        moveSpeed = 4;
    }
    else
    {
        animator.SetBool("isRunning", false);
        moveSpeed = 2;
    }

    //Crouching
    if (Input.GetKey(KeyCode.C))
    {
        animator.SetBool("isCrouching", true);
        moveSpeed = 1;
    }
    else
    {
        animator.SetBool("isCrouching", false);
        moveSpeed = 2;
    }

im newbie at game dev, i'd appreciate any advice

CodePudding user response:

I assume you are not pressing the C and the moveSpeed is "stuck" on 2. If the code is running it runs from top to bottom, so when you are pressing the left shift it sets the move speed to 4. After this is checks the C, when this is not true it will override the 4 with 2.

    if (Input.GetKey(KeyCode.LeftShift))
    {
        animator.SetBool("isRunning", true);
        moveSpeed = 4;
    }
    else if (Input.GetKey(KeyCode.C))
    {
        animator.SetBool("isCrouching", true);
        moveSpeed = 1;
    }
    else
    {
        animator.SetBool("isCrouching", false);
        animator.SetBool("isRunning", false);
        moveSpeed = 2;
    }
  • Related