I have a rigidbody first person character. To move, you click and hold the mouse and rigidbody.AddForce will accelerate character in that direction. This mostly works fine, but when the player looks up and clicks, they are able to fly.
I want to prevent rigidbody.AddForce from applying any force to the global Y axis. Freezing the position on the Y axis on the rigidbody is almost exactly what I want, but it means the level must be completely flat and the character wont be able to move up or down slopes.
Is there a way to only apply force to the X and Z axis? Here’s what I’m currently using:
function FixedUpdate () {
if (Input.GetMouseButton (0))
{
rigidbody.AddForce(-transform.forward * 50);
}
}