Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question Gravity pull not taken into account in Physics2D.simulate

Discussion in 'Physics' started by portalobbqueen, May 9, 2023.

  1. portalobbqueen


    Apr 28, 2023
    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:

    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:

    Code (CSharp):
    1. public void SimulateLaunch(Transform player, Vector3 force)
    2.     {
    4.         _simulatedPlayer.transform.position = player.position;
    5.         _simulatedPlayer.transform.rotation = player.rotation;
    6.         _simulatedPlayer.GetComponent<Rigidbody2D>().velocity =;
    8.         if(_lastforce != force)
    9.         {
    10.             _simulatedPlayer.GetComponent<Rigidbody2D>().AddForce(force);
    11.             for (int i = 0; i < _maxSimulSteps; i++)
    12.             {
    13.                 _physicsScene.Simulate(Time.fixedDeltaTime);
    15.                 points[i] = _simulatedPlayer.transform.position;
    16.                 trajectoryLine.SetPosition(i, points[i]);
    17.             }
    18.         }
    19.         _lastforce = force;
    21.     }

    However, I have GameObjects representing planets that exert a gravity pull on my player.
    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    5.         float distance = Vector2.Distance(player.transform.position, transform.position);
    6.         if (distance < influenceRange)
    7.         {
    8.             pullForce = (transform.position - player.transform.position).normalized * intensity * fixedUpdateCompensator / distance;
    9.             player.GetComponent<Rigidbody2D>().AddForce(pullForce, ForceMode2D.Force);
    10.         }
    13.         float distanceToSim = Vector2.Distance(playerSimulation.transform.position, transform.position);
    14.         if (distanceToSim < influenceRange)
    15.         {
    16.             playerSimulation.GetComponent<Rigidbody2D>().AddForce(((transform.position - playerSimulation.transform.position).normalized * intensity * fixedUpdateCompensator / distanceToSim), ForceMode2D.Force);
    17.         }
    20.     }

    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:

    Code (CSharp):
    1. if (distanceToSim < influenceRange)
    2.         {
    3. //Log something
    4.         }
    It pops in the console.

    Any help appreciated, even if it's " read this part of the documentation" :)

    Attached Files:

  2. MelvMay


    Unity Technologies

    May 24, 2013
    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.
    portalobbqueen likes this.
  3. MelvMay


    Unity Technologies

    May 24, 2013
    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.
    Edy and portalobbqueen like this.