Getting final position after rigidbody.addForce

Hi,
Can I get / predict the final position of a rigidbody after addForce has been applied?
Thank you.

You can’t easily predict the future, under any circumstances. However, assuming that the rigidbody keeps a straight ballistic trajectory, it is possible to extrapolate its eventual resting place by stepping it out. There is a nice script on the Unify Wiki that can do this. I’d link you to it, but the entire website is down at the moment!

The script basically works by predicting (by raytracing) how an object would move in one frame (assuming constant gravity and no external forces) from its current velocity. To calculate the resultant velocity from an impulse, use this equation-

velocity = momentum / mass;

Which means that a change in velocity will be equal to the ‘force’ value in the AddForce function divided by the rigidbody’s mass.

Every ‘step’, it moves the ‘predicted position’ by the velocity / physics timestep (Time.fixedDeltaTime), and uses Physics.Linecast to determine if it hits anything. To calculate the eventual landing point, you can simulate steps until the ‘predicted’ object strikes the ground.

This script simulate it in a simple way but expanding on it requires a little Physics research
trajectory simulation

Keep in mind that in many situations rolling your own simple physics stuff will make life much easier, it doesn’t need to be super complicated to make a fun gameplay.

To estimate the position after adding addforce (with rough accuracy) you can just play with the numbers until you find the right combination.

   Vector3 force = dir; //whatever force your adding
   rigidbody.AddForce(force);

   //Starting point
   var pos = transform.position;
   pos += (guess * 0.02f); //This will depend on other factors in your physics but a starting point.

  //Add this section below if you want to account for gravity (ie ball coming back down)
   float ydif = pos.y - act_s.db_tras[0].position.y;
   var extra_y = guess * 0.0037f * ydif; //Depends on your gravity
 //This removes negatives (ie ball going straight to ground and bouncing back up, it won't be perfect but depends on your needs
   if (ydif < 0)
   extra_y = new Vector3(0, 0, 0);
   pos += extra_y;
   //If you want the prediction to be the height of your starting point (ie ground level)
   pos.y = transform.position.y;
    //final result is in pos

Those 2 numbers 0.02f for distance and 0.0037f for height/gravity are what you want to try different combinations with.

This won’t be perfect, but it will be close for enough depending on the situation.

The code to get the position of an object is:

var CurrentPosition : Vector3;
var Transformposition : Vector3;    

function Start(){
  CurrentPosition = rigidbody.position; //Gives position of objects rigidbody

  Transformposition = transform.position;  //Gives position of parent object
}

The full syntax is: Vector3() = [Objectname].transform.position; for transform. If objectname is omitted, the object is used the current script is attached to. Also mind the usage of lowercase letters, while Transform also exists but has a different effect. Don’t know the difference between rigidbody.position and transform.position since both are applied to the same object - but you asked the question.

There also is a local-version of positions. Search the documentation for this: Unity-documentation

You can use rigidbody.position