Here is my third-person movement script:
float h = Input.GetAxis("leftH");
float v = Input.GetAxis("leftV") * -1;
Vector3 newDirection = new Vector3(h, 0, v);
Vector3 newDirectionInWorld = Camera.main.transform.TransformDirection(newDirection);
newDirectionInWorld *= moveSpeed;
newDirectionInWorld.y -= gravity * Time.deltaTime;
charController.Move(newDirectionInWorld * Time.deltaTime);
when i look down my character walks slower, why?
It is happening because your character wants to walk down/up when looking up/down. I have one trick to prevent this weird behaviour (Alghoritm):
- Set capsule rotation (if player model is capsule etc.) to x rotation of a camera (in euler angles)
- Get capsule transform.forward vector
- Multiply it by walk speed and delta time (for better movement)
- Your character is ready to test
You can also set rotation of player entire game object. This is only trick.