Rigidbody2D Addforce not working on a simulation physics scene
Hello,
Im struggling trying to draw a trajectory line on my project, i just followed a tutorial to do that, im throwing a ball based on a Vector2 force that i calculate from the mouse position after dragging and releasing the click. It works fine on my main scene, but in the script where i instantiate my gameobjects to simulate and get the trajectory, it seems that the instance of the ball is not getting any force applied, because its position doesnt change at all.
public class Projection : MonoBehaviour
{
private Vector2[] testList;
// Start is called before the first frame update
private void Start()
{
CreatePhysicsScene();
testList = new Vector2[200];
}
private Scene _simulationScene;
private PhysicsScene2D _physicsScene;
[SerializeField] private Transform planetsParent;
void CreatePhysicsScene()
{
_simulationScene = SceneManager.CreateScene("LineSimulation", new CreateSceneParameters(LocalPhysicsMode.Physics2D));
_physicsScene = _simulationScene.GetPhysicsScene2D();
foreach(Transform obj in planetsParent)
{
var ghostObj = Instantiate(obj.gameObject, obj.position, obj.rotation);
ghostObj.GetComponent<Renderer>().enabled = false;
SceneManager.MoveGameObjectToScene(ghostObj, _simulationScene);
}
}
[SerializeField] private LineRenderer line;
[SerializeField] private int _maxIterations = 200;
private Vector3 auxFuerza;
public void simulateTrajectory(Ball bola,Vector2 fuerza)
{
var ghostObj = Instantiate(bola, bola.transform.position, bola.transform.rotation);
SceneManager.MoveGameObjectToScene(ghostObj.gameObject, _simulationScene);
line.enabled = true;
ghostObj.rb.AddForce(fuerza * ghostObj.forceMultiplier, ForceMode2D.Impulse);
//ghostObj.Golpe(fuerza);
line.positionCount = _maxIterations;
for(int i = 0; i < _maxIterations; i++)
{
_physicsScene.Simulate(Time.fixedDeltaTime);
testList[i] = ghostObj.transform.position;
line.SetPosition(i, ghostObj.transform.position);
}
Destroy(ghostObj.gameObject);
}
The simulation is done in the simulateTrajectory function, i created the testList variable just or verifyng if the position was changing, but all the values stored there after the for cylce were the same.
Comment
Your answer
