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

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

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

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

>Solution :

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;
    }
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