title: Predict the position of a Rigidbody Object in x second
body: <p>Let's say that you have a <code>Rigidbody</code> Object that moves. Force is added to this Object via <code>Rigidbody.AddForce</code> or <code>Rigidbody.velocity</code>. The Object can roll hit another Object and change direction.</p>
  
  <p>I know about <a href="https://en.wikipedia.org/wiki/Extrapolation" rel="nofollow noreferrer">Extrapolation</a> but in this case, it's nearly impossible to use some formula to obtain the position of the object in <strong>x</strong> seconds, since the Object can hit another object and change speed/direction in the process.</p>
  
  <p>Unity 2017 introduced <a href="https://docs.unity3d.com/2017.1/Documentation/ScriptReference/Physics-autoSimulation.html" rel="nofollow noreferrer"><code>Physics.autoSimulation</code></a> and <a href="https://docs.unity3d.com/2017.1/Documentation/ScriptReference/Physics.Simulate.html" rel="nofollow noreferrer"><code>Physics.Simulate</code></a> to solve this problem. For 2D physics, that is <a href="https://docs.unity3d.com/2017.1/Documentation/ScriptReference/Physics2D-autoSimulation.html" rel="nofollow noreferrer"><code>Physics2D.autoSimulation</code></a> and <a href="https://docs.unity3d.com/2017.1/Documentation/ScriptReference/Physics2D.Simulate.html" rel="nofollow noreferrer"><code>Physics2D.Simulate</code></a>. All I did was first set <code>Physics.autoSimulation</code> to false then call the <code>Physics.Simulate</code> function.</p>
  
  <hr>
  
  <p>In my example, I wanted to know where a <code>Rigidbody</code> would be in <code>4</code> seconds after adding force to it, it seems to work fine for tiny seconds like <code>1</code>. The problem is that when I pass in bigger numbers like <code>5</code> and above, to the <code>Simulate</code> function, the predicted position is <strong>not</strong> accurate. It's way way off. </p>
  
  <p>Why is this happening and how can I fix it? This problem is worse on Android devices.</p>
  
  <p>My current Unity version is <em>Unity 2017.2.0b5</em>.</p>
  
  <p>Below is the sample code I am using. The <code>guide</code> GameObject is simply used to display/show where that predicted position is.</p>
  
  <pre><code>public GameObject bulletPrefab;
  public float forceSpeed = 50;
  
  public GameObject guide;
  
  // Use this for initialization
  IEnumerator Start()
  {
      //Disable Physics AutoSimulation
      Physics.autoSimulation = false;
  
      //Wait for game to start in the editor before moving on(NOT NECESSARY)
      yield return new WaitForSeconds(1);
  
      //Instantiate Bullet
      GameObject obj = Instantiate(bulletPrefab);
  
      Rigidbody bulletRigidbody = obj.GetComponent&lt;Rigidbody&gt;();
  
      //Calcuate force speed. (Shoot towards the x + axis)
      Vector3 tempForce = bulletRigidbody.transform.right;
      tempForce.y += 0.4f;
      Vector3 force = tempForce * forceSpeed;
  
      //Addforce to the Bullet
      bulletRigidbody.AddForce(force, ForceMode.Impulse);
  
      //yield break;
      //Predict where the Rigidbody will be in 4 seconds
      Vector3 futurePos = predictRigidBodyPosInTime(bulletRigidbody, 4f);//1.3f
      //Show us where that would be
      guide.transform.position = futurePos;
  }
  
  Vector3 predictRigidBodyPosInTime(Rigidbody sourceRigidbody, float timeInSec)
  {
      //Get current Position
      Vector3 defaultPos = sourceRigidbody.position;
  
      Debug.Log("Predicting Future Pos from::: x " + defaultPos.x + " y:"
          + defaultPos.y + " z:" + defaultPos.z);
  
      //Simulate where it will be in x seconds
      Physics.Simulate(timeInSec);
  
      //Get future position
      Vector3 futurePos = sourceRigidbody.position;
  
      Debug.Log("DONE Predicting Future Pos::: x " + futurePos.x + " y:"
          + futurePos.y + " z:" + futurePos.z);
  
      //Re-enable Physics AutoSimulation and Reset position
      Physics.autoSimulation = true;
      sourceRigidbody.velocity = Vector3.zero;
      sourceRigidbody.useGravity = false;
      sourceRigidbody.position = defaultPos;
  
      return futurePos;
  }
  </code></pre>