Multiple properties for bool? (Trying to prevent backward sprinting)

I want to edit the FirstPersonController script to make it impossible to sprint backwards.
I found the part of the script talking about sprinting and it looked like this :

        private void GetInput(out float speed)
        {
            // Read input
            float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
            float vertical = CrossPlatformInputManager.GetAxis("Vertical");

            bool waswalking = m_IsWalking;

#if !MOBILE_INPUT
            // On standalone builds, walk/run speed is modified by a key press.
            // keep track of whether or not the character is walking or running
            m_IsWalking = !Input.GetKey(KeyCode.LeftShift);


#endif
            // set the desired speed to be walking or running
            speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
            m_Input = new Vector2(horizontal, vertical);

            // normalize input if it exceeds 1 in combined length:
            if (m_Input.sqrMagnitude > 1)
            {
                m_Input.Normalize();
            }

            // handle speed change to give an fov kick
            // only if the player is going to a run, is running and the fovkick is to be used
            if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
            {
                StopAllCoroutines();
                StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
            }
        }

As you can see, there’s a line saying that the character will walk if the LeftShift key isn’t pressed (Line 12 on the post, line 213 on the original and complete file).

The line in question is this : m_IsWalking = !Input.GetKey(KeyCode.LeftShift);

The problem is that I can’t find the way to add another propertie for m_IsWalking. I want to make it walk when the LeftShift and S keys are pressed togheter. It seems like bool stuffs aren’t allowing multiples properties at a time.

Anyone knows how to do it?

It’s already checking to see if you are pressing the sprint button, just add a condition to check if you’re pressing the back button (KeyCode.S).

1 Like

This.

For more complex behaviour finite state machines are a good idea.

That’s exactly what I’m trying to do but apparently, bools can’t have more than one condition. Can you send an example?

bool isRunning;
bool isJumping;

if (isRunning && isJumping){
    Debug.Log("That's flying!");
}

Thanks for the help, but jumping isn’t my problem here…

think he was just showing u how to use 2 bools…

1 Like

Yup. I generally assume a base level of intelligence if someone is trying to learn to program.

But anyways, it would look like this in the OP

m_IsWalking = Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.S);

edit: It is worth mentioning you might want to use axes instead. Hard coding to specific keys has burnt me every time.

1 Like

Now my character just walks when im pressing shift and is running when im not

Seems to meet specification for me… I’m slightly lost here as to what you want to achieve. Might mean it’s time for me to go to bed.

You’ll want to include the ! before left shift. It is checking to see if you are sprinting. If you are (true), then the ! instructs your m_IsWalking bool to be false (opposite of sprint’s true output). I would try something like thism_IsWalking = !Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.S);:

1 Like

I did not got what you were saying but you are using out with speed. And the methods is returning nothing. You should always return a value unless you want to return more that a value.

THANK YOU! It worked. It may be because of those two line || you put between the two conditions. What do they mean? It may be useful for my future codes.

Or

ok thanks