Day 132 — Fixing Player Movement
Hey and welcome!
Today we’re going to fix the movement of the player so that they’ll move in the direction that they’re facing. As a bonus we’ll also look into adding camera sensitivity and the ability to lock the cursor to the game screen so that we can move the camera better.
Currently the issue with our movement is that we’re moving globally instead of locally. So if we press the W or Up arrow key the player is going to move forward relative to the world space/environment that they’re in rather than where the camera/player is currently facing.
Let’s update the movement code in our Player script to fix this issue:
private void Movement()
if (_controller.isGrounded == true)
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
_direction = new Vector3(horizontal, 0, vertical);
_velocity = _direction * _speed; if (Input.GetKeyDown(KeyCode.Space))
_velocity.y = _jumpHeight;
} _velocity.y -= _gravity * Time.deltaTime; _velocity = transform.TransformDirection(_velocity); _controller.Move(_velocity * Time.deltaTime);
As you can see here all that we need is just the one line of code and that will fix the issue! TransformDirection is a pretty handy method as what it does is transform a direction from local space to world space. In this case we pass in our velocity since it’s our direction multiplied by the speed.
With that line in place you should have the above working for you where if you’re moving forward you will always head in the direction that your player is facing.
You may find that you’re not happy with the current sensitivity of the camera so next up we’ll look into fixing that. Go ahead and add in this code to your Player script:
private float _cameraSensitivity = 2.0f;private void Rotation()
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y"); Vector3 currentRotation = transform.localEulerAngles;
currentRotation.y += mouseX * _cameraSensitivity;
transform.localRotation = Quaternion.AngleAxis(currentRotation.y, Vector3.up); Vector3 currentCameraRotation = _mainCamera.gameObject.transform.localEulerAngles;
currentCameraRotation.x -= mouseY * _cameraSensitivity;
_mainCamera.gameObject.transform.localRotation = Quaternion.AngleAxis(currentCameraRotation.x, Vector3.right);
As you can see here it’s a nice easy fix to implement, all we’re doing is storing a float variable for the camera sensitivity which we use to multiply the value that gets returned from mouseX and mouseY. Be sure to adjust this value to your liking in the inspector!
Lastly let’s go ahead and add in this bit of code to lock our cursor when the game is running:
Cursor.lockState = CursorLockMode.Locked;
Cursor.lockState = CursorLockMode.None;
Luckily for us Unity has a built-in way to determine the lock state of our cursor which we can set by using CursorLockMode.Locked to lock and hide it and Cursor.LockMode.None to unlock and make it visible again.
Make sure you don’t forget to include the option to exit the locked state otherwise you’re going to lose control of your cursor until you force the game to close!