Search Unity

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

Discussion in 'Scripting' started by DrBibop, Oct 9, 2015.

  1. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    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 :

    Code (CSharp):
    1.         private void GetInput(out float speed)
    2.         {
    3.             // Read input
    4.             float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    5.             float vertical = CrossPlatformInputManager.GetAxis("Vertical");
    6.  
    7.             bool waswalking = m_IsWalking;
    8.  
    9. #if !MOBILE_INPUT
    10.             // On standalone builds, walk/run speed is modified by a key press.
    11.             // keep track of whether or not the character is walking or running
    12.             m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    13.  
    14.  
    15. #endif
    16.             // set the desired speed to be walking or running
    17.             speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    18.             m_Input = new Vector2(horizontal, vertical);
    19.  
    20.             // normalize input if it exceeds 1 in combined length:
    21.             if (m_Input.sqrMagnitude > 1)
    22.             {
    23.                 m_Input.Normalize();
    24.             }
    25.  
    26.             // handle speed change to give an fov kick
    27.             // only if the player is going to a run, is running and the fovkick is to be used
    28.             if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    29.             {
    30.                 StopAllCoroutines();
    31.                 StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    32.             }
    33.         }

    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?
     
  2. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    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).
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This.

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

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    That's exactly what I'm trying to do but apparently, bools can't have more than one condition. Can you send an example?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code (CSharp):
    1. bool isRunning;
    2. bool isJumping;
    3.  
    4. if (isRunning && isJumping){
    5.     Debug.Log("That's flying!");
    6. }
     
  6. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    Thanks for the help, but jumping isn't my problem here...
     
  7. Reaxgrim

    Reaxgrim

    Joined:
    Feb 15, 2014
    Posts:
    16
    think he was just showing u how to use 2 bools...
     
    Kiwasi likes this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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

    Code (csharp):
    1. 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.
     
    Tiki likes this.
  9. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    Now my character just walks when im pressing shift and is running when im not
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  11. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    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 this
    Code (CSharp):
    1. m_IsWalking = !Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.S);
    :
     
    DrBibop likes this.
  12. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    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.
     
  13. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    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.
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Or
     
  15. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    ok thanks