Newtonian Gravity working backwards

I’m trying to make a planet affect other objects.

It kind of resembles artificial planetary gravity, but it is wierd, because the farther the object is, the more force it excerts. Perhaps there is something wrong with my formulation?

Vector3 direction = rbUnderInfluence *.transform.position - transform.position;*

intensity = gravitationalForce * (rb.mass * planetMass / distance * distance);

rb.AddForce (direction * intensity);

You have two errors here:

  • This planetMass / distance * distance will be equal to planetMass. Division and multiplication have the same precedence. [Operators with same precedence are executed left to right][1]. You would need to put your “distance * distance” into brackets.
  • The second problem is that you use your direction vector directly. This vector has a “length”. The further away your objects are, the “longer” / greater the vector is. You have to normalize the vector.

All in all this should work:

Vector3 direction = rbUnderInfluence *.transform.position - transform.position;*

intensity = gravitationalForce * rb.mass * planetMass / (distance * distance);

rb.AddForce (direction.normalized * intensity);
Note that using real world values will most likely create huge errors due to the large numbers.
Finally instead of your distance * distance you could simply use direction.sqrMagnitude. It’s the distance / length of the direction vector squared.
[1]: https://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx