When I use physics.simulate to calculate end position of my gameobject it shows correct position only when enough force is given. If I push my object with middle or strong force end position showed by physics. simulate is very accurate, but when I push my gameobject only a little physics.simulate shows position too far, like I would give higher force.
RollerMove Pushes gameobject, checkposition checks end position, simulatephysics invokes.
private void simulatephysics()
{
touchhandler.listofcoll.Clear();
touchhandler.hitramp = false;
touchhandler.force = (float)Math.Pow(distanceToRoller, 3);
touchhandler.RollerMove();
endpos = touchhandler.CheckPosition(rollerobj.GetComponent<Rigidbody>());
destination.transform.position = endpos;
markeractivated = true;
}
public void RollerMove()
{
if (force > 100f)
{
force = 100f;
}
transform.GetComponent<Rigidbody>().AddForce(raydir *force* 15f , ForceMode.Force);
}
public float timeCheck = 8f;
public Vector3 CheckPosition(Rigidbody defaultRb)
{
Physics.autoSimulation = false;
defaultRb = GetComponent<Rigidbody>();
Vector3 defaultPos = defaultRb.position;
Quaternion defaultRot = defaultRb.rotation;
float timeInSec = timeCheck;
while (timeInSec >= 0)
{
timeInSec -= 0.05f;
Physics.Simulate(Time.fixedDeltaTime);
}//end while
Vector3 futurePos = defaultRb.position;
defaultRb.velocity = Vector3.zero;
defaultRb.angularVelocity = Vector3.zero;
defaultRb.transform.position = defaultPos;
defaultRb.transform.rotation = defaultRot;
Physics.autoSimulation = true;
return futurePos;
}