Search Unity

2D changing direction of player

Discussion in 'Editor & General Support' started by mrkennedie, Feb 18, 2019.

  1. mrkennedie

    mrkennedie

    Joined:
    Jan 11, 2019
    Posts:
    2
    Hello,

    im a beginner and need some help. Im following a 2D game tutorial where the player should change the direction he is looking at, when pressing left or right movement key.

    For this the Tutor made a boolian variable called "LookingRight" with true on default. (private bool lookingRight = true; )

    In the picture below you can see the function called Flip, which enables the player to change the direction. So far its not a problem to understand this. But a little bit above, inside the FixedUpdate-Function, i dont understand the for-loop. How does unity know that player is looking in the right direction if horizontal key is positive, and looking in the right direction if horizontal key got a negative value? Isnt it necessary to tell the programm first that if the horizontalbutton is pressed in the right direction (hor = positive), the boolian lookingRight is true? and if this is not the case, the Flip function should engage?

    i mean its just a && Operator. Hw does unity know that if hor >0 lookingRight is true?

    Would be very happy if someone could help me.
    Thank you!
     
  2. mrkennedie

    mrkennedie

    Joined:
    Jan 11, 2019
    Posts:
    2
    Here again the picture. Help.jpg