I have a gameobject, when player chooses force and direction there are drawn lines of movement trajectory and end position using physics.simulate. If I use it every 0.5f seconds it works well, but I have to predict trajectory and end position every frame, but then the game is lagging. How can I predict trajectory and end position every frame?
private IEnumerator destcor()
{
while (true)
{
yield return new WaitForSeconds(0.3f);
touchhandler.listofcoll.Clear();
touchhandler.force = (float)Math.Pow(distanceToRoller, 3);
touchhandler.RollerMove();
endpos = touchhandler.CheckPosition(rollerobj.GetComponent<Rigidbody>());
destination.transform.position = endpos;
}
}
public Vector3 CheckPosition(Rigidbody defaultRb)
{
Physics.autoSimulation = false;
defaultRb = GetComponent<Rigidbody>();
Vector3 defaultPos = defaultRb.position;
Quaternion defaultRot = defaultRb.rotation;
float timeInSec = timeCheck;
while (timeInSec >= Time.fixedDeltaTime)
{
timeInSec -= Time.fixedDeltaTime;
Physics.Simulate(Time.fixedDeltaTime);
}//end while
Vector3 futurePos = defaultRb.position;
Physics.autoSimulation = true;
defaultRb.velocity = Vector3.zero;
defaultRb.angularVelocity = Vector3.zero;
defaultRb.transform.position = defaultPos;
defaultRb.transform.rotation = defaultRot;
return futurePos;
}