I’m trying to make a 2D game where you can shoot missiles at planets but the planets have 2d point effectors so if the missile comes within the range of the planet it gets attracted towards it. To aim I’m trying to predict the trajectory of the missile using a physics scene simulation mostly following the video Trajectory prediction in Unity - YouTube .
The creation of the physics scene:
CreateSceneParameters parameters = new CreateSceneParameters(LocalPhysicsMode.Physics2D);
predictionScene = SceneManager.CreateScene("Prediction", parameters);
predictionPhysicsScene = predictionScene.GetPhysicsScene2D();
Physics2D.autoSimulation = false;
currentScene = SceneManager.GetActiveScene();
currentPhysicsScene = currentScene.GetPhysicsScene2D();
I manually simulate the current physics scene in the fixedupdate.
Then I move a dummy missile and dummy planets (same as the regular missiles and planets but without sprites) using the MoveGameObjectToScene function
And finally to simulate the missiles:
LineRenderer lineRenderer = currentDummyMissile.GetComponent<LineRenderer>();
lineRenderer.positionCount = numIterations;
dummyRB.AddForce(Force, ForceMode2D.Impulse); //Launching the missile (this works)
for (int i=0; i<numIterations; i++) {
predictionPhysicsScene.Simulate(Time.fixedDeltaTime);
lineRenderer.SetPosition(i, currentDummyMissile.transform.position);
}
For some reason though the point effectors don’t seem to have an effect on the physics simulated dummy missile. Are they not simulated when you call the Simulate function? If not is there a better way to predict the trajectory of the missile?
I have also tried manually creating the effect of the point effector by scripting it out and adding force inside the OnTriggerStay2D function but that also has no effect.