# Gravity pull not taken into account in Physics2D.simulate

Hello! I’m new to Unity and I think I’m not getting how Physics2D.simulate works.

I’m trying to implement a trajectory prediction in a 2D outer space game with line renderer using the physics simulation method presented here: Unity Trajectory Prediction (Simulation Method) | by Austin Mackrell | Medium

I implemented as in the post: a physicscene copy of the actual scene is created, clones of the planets and the player are moved there.

Then I run physicsScene2D.simulate method X iterations in the future to make my prediction when the player aims and the line renderer draws at each iteration the simulated player’s position.

Here is the simulated launch code:

``````public void SimulateLaunch(Transform player, Vector3 force)
{

_simulatedPlayer.transform.position = player.position;
_simulatedPlayer.transform.rotation = player.rotation;
_simulatedPlayer.GetComponent<Rigidbody2D>().velocity = Vector3.zero;

if(_lastforce != force)
{
_simulatedPlayer.GetComponent<Rigidbody2D>().AddForce(force);
for (int i = 0; i < _maxSimulSteps; i++)
{
_physicsScene.Simulate(Time.fixedDeltaTime);

points[i] = _simulatedPlayer.transform.position;
trajectoryLine.SetPosition(i, points[i]);
}
}
_lastforce = force;

}
``````

However, I have GameObjects representing planets that exert a gravity pull on my player.

``````void FixedUpdate()
{

float distance = Vector2.Distance(player.transform.position, transform.position);
if (distance < influenceRange)
{
pullForce = (transform.position - player.transform.position).normalized * intensity * fixedUpdateCompensator / distance;
player.GetComponent<Rigidbody2D>().AddForce(pullForce, ForceMode2D.Force);
}

float distanceToSim = Vector2.Distance(playerSimulation.transform.position, transform.position);
if (distanceToSim < influenceRange)
{
playerSimulation.GetComponent<Rigidbody2D>().AddForce(((transform.position - playerSimulation.transform.position).normalized * intensity * fixedUpdateCompensator / distanceToSim), ForceMode2D.Force);
}

}
``````

Every FixedUpdate, these planets calculate the force to exert using a simplified newton’s law and addForce to the player’s rigidbody.

When aiming: the line renderer is not curved when near to a planet.
But in my actual scene the ship is gravity pulled correctly.

I think i’m maybe missing something about the physics simulate lifecycle and callbacks handling ?
The fixedUpdate seems to be called because if I log inside:

``````if (distanceToSim < influenceRange)
{
//Log something
}
``````

It pops in the console.

Any help appreciated, even if it’s " read this part of the documentation"

Physics2D.Simulate is not a special code-path. This is what Unity will call automatically when you’re not simulating. It’s absolutely identical no matter who calls it.

1 Like

I think you’re under the assumption that physics calls FixedUpdate, it doesn’t.

FixedUpdate calls (amongst other things) physics. Calling Physics2D.Simulate doesn’t call FixedUpdate. It simulates the physics scene only, not everything else in the Unity scene associated with it.

2 Likes