title: Unity LookAt, but instead rotate entire body towards direction in 3d space
body: <p>Wasted many hours trying to figure out the rotations and many hours looking for answers, but wasn't able to find anything that fits my problem. I need to rotate an entire gameObject to a specific direction rather than rotating in y axis:</p>
  
  <p><img src="https://i.stack.imgur.com/CIDL8.png" alt="Examples of wanted rotations"></p>
  
  <p>1) How the object is currently rotated while given a direction inside Quaternion.LookRotation or by Atan2.
  2,3) Examples of how it should rotate. The red ot simbolizes the pivot point from which the rotation happens</p>
  
  <p>Not much code to show as there is not much to it besides gameObject transformations which are rotated and a direction in which to rotate gameObject.</p>
  
  <p>As requested</p>
  
  <pre><code>[System.Serializable]
  public class ToRotate
  {
      //Object which will be rotated by the angle
      public GameObject gameObject;
      //Object last known position of this object. The object is rotated towards it's last global position
      private Vector3 lastPosition;
  
      //Initializes starting world position values to avoid a strange jump at the start.
      public void Init()
      {
          if (gameObject == null)
              return;
  
          lastPosition = gameObject.transform.position;
      }
  
      //Method which updates the rotation
      public void UpdateRotation()
      {
          //In order to avoid errors when object given is null.
          if (gameObject == null)
              return;
          //If the objects didn't move last frame, no point in recalculating and having a direction of 0,0,0
          if (lastPosition == gameObject.transform.position)
              return;
  
          //direction the rotation must face
          Vector3 direction = (lastPosition - gameObject.transform.position).normalized;
  
          /* Code that modifies the rotation angle is written here */
  
          //y angle
          float angle = Mathf.Rad2Deg * Mathf.Atan2(direction.x, direction.z);
  
          Quaternion rotation = Quaternion.Euler(0, angle, 0);
          gameObject.transform.rotation = rotation;
  
          lastPosition = gameObject.transform.position;
      }
  }
  </code></pre>