Day 132 — Fixing Player Movement

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);
}
[SerializeField]
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);
}
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store