title: Make Character Able To Turn Back and Fourth in Unity
body: <p>I was trying to code a system where a 2D character's sprite and collider turn to face the direction being input (i.e. pressing the left key makes the character face left, pressing the right key makes the character turn right). I had this script and it seemed to work for a while:</p>
  <pre><code>public bool turnedAround = false; //Says if you are facing to the left (true) or right (false)
      [SerializeField]
      private float xVelocityTolerance = .6f; //The slowest the player's X can move at to have the running animation show
      [SerializeField]
      private float groundDetectionOffset = .3f; //How far the raycast origin is offset
      private Animator anim;
      private Rigidbody2D rb;
      private PlayerController playerController;
  
      public float HalfPlayerHeight
      {
          get
          {
              return playerController.halfPlayerHeight;
          }
      }
  
      private bool IsOnGround() //Checks if there is ground below the outside to the left, middle, and outside to the right
      {
          return Physics2D.Raycast(transform.position - new Vector3(groundDetectionOffset, 0f, 0f), -transform.up, HalfPlayerHeight) || Physics2D.Raycast(transform.position, -transform.up, HalfPlayerHeight) || 
              Physics2D.Raycast(transform.position + new Vector3(groundDetectionOffset, 0f, 0f), -transform.up, HalfPlayerHeight); //Uses an artificial line break
      }
  
      void Start()
      {
          anim = GetComponent&lt;Animator&gt;();
          rb = GetComponent&lt;Rigidbody2D&gt;();
          playerController = GetComponent&lt;PlayerController&gt;();
      }
  
      // Update is called once per frame
      void Update()
      {
  
          //NOTE ABOUT WHOLE SECTION OF IS ELSE: The if checks if the player's facing right and moving left. If it is then it turns left and changes turnedAround to be true which means it is facing left. The else if is the opposite
          if (rb.velocity.x &lt; -xVelocityTolerance &amp;&amp; !turnedAround)
          {
              transform.Rotate(0, 180, 0);
              turnedAround = true;
          }
          else if (rb.velocity.x &gt; xVelocityTolerance &amp;&amp; turnedAround)
          {
              transform.Rotate(0, -180, 0);
              turnedAround = false;
          }
  
          /*NOTE ABOUT WHOLE SECTION OF IS ELSE: The if checks if the player is moving either left and right fatser than the VelocityTolerance and if the player isn't jumping/falling.
          If true it will tell the animator by setting IsRunningNotJumping to true. If it it false it will be set to false.*/
          if ((rb.velocity.x &gt; xVelocityTolerance || rb.velocity.x &lt; -xVelocityTolerance) &amp;&amp; IsOnGround() &amp;&amp; !anim.GetBool(&quot;IsRunningNotJumping&quot;))
          {
              anim.SetBool(&quot;IsRunningNotJumping&quot;, true);
          }
          else if (anim.GetBool(&quot;IsRunningNotJumping&quot;))
          {
              anim.SetBool(&quot;IsRunningNotJumping&quot;, false);
          }
  
          //NOTE ABOUT WHOLE SECTION OF IS ELSE: The if statement checks if the player is jumping/falling. If they are than it will tell the animator by setting IsJumpingOrFallling to true and vice versa with the else.
          if (!IsOnGround() &amp;&amp; !anim.GetBool(&quot;IsJumpingOrFalling&quot;))
          {
              anim.SetBool(&quot;IsJumpingOrFalling&quot;, true);
          }
          else if (IsOnGround() &amp;&amp; anim.GetBool(&quot;IsJumpingOrFalling&quot;))
          {
              anim.SetBool(&quot;IsJumpingOrFalling&quot;, false);
          }
  
          if (Fireballs.HasFireballs != anim.GetBool(&quot;HasFireballs&quot;))
          {
              anim.SetBool(&quot;HasFireballs&quot;, Fireballs.HasFireballs); //Sets the HasFireballs bool in the animator equal to the HasFireball bool in the Fireballs script
          }
  
          if (BonusLife.hasBonusLife != anim.GetBool(&quot;HasBonusLife&quot;))
          {
              anim.SetBool(&quot;HasBonusLife&quot;, BonusLife.hasBonusLife); //Sets the HasBonusLife bool in the animator equal to the HasBonusLife bool in the BonusLife script
          }
  
          if (SpawnFireballs.isThrowing != anim.GetBool(&quot;IsThrowing&quot;))
          {
              anim.SetBool(&quot;IsThrowing&quot;, SpawnFireballs.isThrowing); //Sets the IsThrowing bool in the animator equal to the IsThrowing bool in the SpawnFireballs script
          }
      }
  }
  </code></pre>
  <p>(rb = RigidBody2d)
  However, for some reason now when I try to walk in one direction the TurnedAroud bool would constantly switch on and off and the player would be unable to move except in weird and unexpected ways. How can I fix this? I am open to different ways of making the Player face the correct direction.</p>
  <p>I am coding in c# in Unity 2020.3.24f1 using visual studio. I am new to stack exchange so if I asked this question wrong I am sorry.</p>