My player in my game has a rigidbody attatched to him. I use the AddForce function to move him around. I also have a terrain set up. My drag variable of the rigidbody is set to 4 and it has a mass of 1. Gravity is true. The Gravity var moved my player to slowly so I also added a constant force with a -40y force. Everything works great but when I look down or up the player slowly creeps forward when looking down or backwards when looking up. I look around with the mouselook script from the character controllers package. I was wondering is there a way to stop this from happening. The player has a capsule collider also.
Don’t add force in the direction of transform.forward. If you’re looking straight down, transform.forward.x will be very close to 0, so doing this:
rigidbody.AddForce(new Vector3(transform.forward.x, 0 , transform.forward.z) * 50);
will give you a much lesser speed when looking down than when looking straight ahead.
The easiest thing to do is to remove the y-component from your forward-vector, and then normalize it. That will give you a vector that has constant length, no matter what the tilt of your look is:
Vector3 myForward = transform.forward;
myForward.y = 0f;
myForward = myForward.normalized;
if(Input.GetKey("w") && Input.GetKey(KeyCode.LeftShift) && !water1){
rigidbody.AddForce(new Vector3(myForward.x, 0 , myForward.z) * 50);
}
Do the same thing for transform.right.
I’d also suggest trying to reduce the immense amount of if/elses. Instead of checking left shift and water in every one of those, create a speed variable based on shift and water, and use that for what you multiply your speed with.