title: How can I check if an object is facing a certain direction in Unity C#
body: <p>Still pretty new to Unity and C#. I can usually find my way around when it comes to setting/changing the position of an object, but when it comes to rotation it's still mind boggling to me, with quaternion, eulerangles etc..</p>
  <p>I'm trying to check if an object (a head) is facing a certain direction, if it's not facing that direction it needs to set it to that direction depending which way it goes (up, down, left or right). This is for a snake game (like the old top down nokia game) I'm currently developing.</p>
  <p>I tried to look it up, but none of the answers are specific to what I'm trying to do.</p>
  <p>As of right now this is what I have for player input (a snippet of code):</p>
  <pre><code>private int GetAxisRaw(Axis axis)
      {
          if (axis == Axis.Horizontal)
          {
              bool left = Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A);
              bool right = Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D);
  
              if (left)
              {
                  snakeHeadRotation.transform.Rotate(0, 180, 0); // Keeps rotating when left is pressed
                  return -1;
              }
              if (right)
              {
                  snakeHeadRotation.transform.Rotate(0, -180, 0);
                  return 1;
              }
  
              return 0;
          }
  </code></pre>
  <p>As you can see the snakeHeadRotation.transform.Rotate(0, 180, 0); is getting called each time the player presses the left key for instance, thus turning the head of the snake even when the snake is still traveling to the left.</p>
  <p>My logic would be to store the rotation value somewhere (type Quaternion/bool maybe) and check to see if that rotation value matches up with the current rotation value set to left (which is snakeHeadRotation.transform.Rotate(0, 180, 0);) If that's the case, don't do anything. If it isn't, set it to snakeHeadRotation.transform.Rotate(0, 180, 0); for instance.</p>
  <p>the code would look something like (writing it out):</p>
  <pre><code>private int GetAxisRaw(Axis axis)
  {
      if (axis == Axis.Horizontal)
      {
          bool left = Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A);
          bool right = Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D);
  
          if (left &amp;&amp; rotation is not equal to snakeHeadRotation.transform.Rotate(0, 180, 0))
          {
              snakeHeadRotation.transform.Rotate(0, 180, 0); 
              return -1;
          }
          if (right)
          {
              snakeHeadRotation.transform.Rotate(0, -180, 0);
              return 1;
          }
  
          return 0;
      }
  </code></pre>
  <p>As said, I'm very unfamiliar with setting or storing rotation values. How can I achieve this? Is this even the right approach or is there a more logical one? I can't figure this one out myself..</p>
  <p>I hope I explained it right, I'm usually terrible at explaining things.</p>