title: How to make a thrown object rotate automatically based on the direction its thrown in?
body: <p>so what I am trying to do is pull a spear back(like a slingshot) and send it flying, all the while making it rotate while it's flying in the air to mimic a real thrown spear. So far, I have only been able to make it rotate after a few seconds to a certain position but I can already tell that this isn't really a permanent fix and will definitely lead to problems down the line. I have tried looking for better ways of rotating the object but I can't really find anybody else who had a problem like this and it's hard to understand the unity documentation regarding rotation as it's for 3D objects.</p>
  <p>Here is a gif showing how it looks like:
  <a href="https://gfycat.com/chiefglasshorsemouse" rel="nofollow noreferrer">https://gfycat.com/chiefglasshorsemouse</a></p>
  <p>This is the code that I have attached to my spear object that's responsible for letting me pull it back and launch it with the mouse:</p>
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.SceneManagement;
  using UnityEngine.Scripting.APIUpdating;
  
  public class spear : MonoBehaviour
  {
      Rigidbody2D rb;
      
      Vector3 velocity;
  
      private Vector3 _initialPosition;
  
      [SerializeField] private float _launchPower = 500;
  
      bool rotating = false;
  
      public GameObject objectToRotate;
  
      private float _timeSittingAround;
  
      private bool _spearWasLaunched;
      [SerializeField] private float _spearRotation = 360;
  
      private void Awake()
      {
          _initialPosition = transform.position;
  
          rb = GetComponent&lt;Rigidbody2D&gt;();
      }
  
      void Start()
      {
          rb = GetComponent&lt;Rigidbody2D&gt;();  
      }
  
     
      //rotates the spear
      IEnumerator rotateObject(GameObject gameObjectToMove, Quaternion newRot, float duration)
      {
          if(rotating)
          {
              yield break;
          }
          rotating = true;
  
          Quaternion currentRot = gameObjectToMove.transform.rotation;
  
          float counter = 0;
          while(counter &lt; duration)
          {
              counter += Time.deltaTime;
              gameObjectToMove.transform.rotation = Quaternion.Lerp(currentRot, newRot, counter / duration);
              yield return null;
          }
  
          
          
              rotating = false;
          
      }
  
      //reloads the scene if spear goes out of bounds or lays dormant for 2 seconds
      void Update()
      {
          GetComponent&lt;LineRenderer&gt;().SetPosition(0, transform.position);
          GetComponent&lt;LineRenderer&gt;().SetPosition(1, _initialPosition);
          
  
  
          if (_spearWasLaunched &amp;&amp; 
              GetComponent&lt;Rigidbody2D&gt;().velocity.magnitude &lt;= 0.1)
          {
              _timeSittingAround += Time.deltaTime;
          }
         
          if(transform.position.y &gt; 30 || 
              transform.position.y &lt; -12.5 ||
              transform.position.x &gt; 40 ||
              transform.position.x &lt; -20 ||
              _timeSittingAround &gt; 2)
          {
              string currentSceneName = SceneManager.GetActiveScene().name;
              SceneManager.LoadScene(currentSceneName);
          }
  
      }
  
      //slingshot mechanic
      private void OnMouseDrag()
      {
          Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
          transform.position = new Vector3(newPosition.x, newPosition.y);
      }
  
      
      private void OnMouseDown()
      {
          GetComponent&lt;SpriteRenderer&gt;().color = Color.red;
          GetComponent&lt;LineRenderer&gt;().enabled = true;
  
      }
  
  
  
      //launches the spear when mouse is released as well as begins the rotating mechanic 
      private void OnMouseUp()
      {
          Vector2 directionToInitialPosition = _initialPosition - transform.position;
  
          GetComponent&lt;Rigidbody2D&gt;().AddForce(directionToInitialPosition * _launchPower);
  
          GetComponent&lt;Rigidbody2D&gt;().gravityScale = 1;
  
          _spearWasLaunched = true;
  
          GetComponent&lt;LineRenderer&gt;().enabled = false;
  
  
  
          Quaternion rotation2 = Quaternion.Euler(new Vector3(0, 0, _spearRotation));
          StartCoroutine(rotateObject(objectToRotate, rotation2, 3f));
  
          
      }
  
  
      
  
  
  
  }
  </code></pre>