Say I have a GameObject with a ridgidbody that is motionless. Given a direction and a force I use addforce() to accelerate the object in that direction. When the GameObject reaches a max velocity the force is no longer applied and the GameObject drifts along its path.
Now say I want to change direction, currently I’m using the ridgidbody’s drag setting to counteract the inertia. This works fine, but the problem is how do I determine when the GameObject is traveling in the new direction. I want the drag to be 0 when the GameObject is traveling in the correct direction so that I don’t have to apply force to keep the object up to speed and the drag to only be applied while the GameObject is changing direction.
Hope i made that clear enough to understand, Thanks.
Update:
This is called in FixedUpdate()
wantedDirection is a raycast direction from the mouse when no object or destination vector is given.
direction = Vector3.MoveTowards(direction, wantedDirection, turnSpeed * Time.deltaTime);
targetRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
if (Vector3.Angle(direction, rigidbody.velocity) < 0.01f)
rigidbody.drag = 1.0f;
else
rigidbody.drag = 0.0f;
if(currentSpeed < currentTopSpeed ){
rigidbody.AddForce(direction * acceleration, ForceMode.Acceleration);
}