i have tried adding Time.deltaTime but that only results in making the object go even slower, when i test the game on the inspector the momentum works just fine, but once i create a build and try the same thing my object goes a lot slower
if (Input.GetKey("w"))
{
rb.AddForce(transform.forward * moveforward);
}
if (Input.GetKey("s"))
{
rb.AddForce(transform.forward * -moveback);
}
1 Answer
1
Call AddForce() inside the FixedUpdate()
bool moveForwardRequest;
void Update()
{
if (Input.GetKey("w"))
{
moveForwardRequest = true;
}
}
void FixedUpdate()
{
if(moveForwardRequest)
{
moveForwardRequest = false;
rb.AddForce(transform.forward * moveforward);
}
}
AddForce have the optional third parameter, it is ForceMode.Force by default, AddForce with ForceMode.Force mode works like this internally,
rigidbody.velocity += Vector3.forward*moveForward* Time.fixedDeltaTime / (rigidbody.mass);
so, it is better to call AddForce method inside FixedUpdate, to avoid unexpected behaviour.