Create third person movement in 5 minutes with a working camera and rotation using your mouse.
I will focus on best practices and explain why things work to teach you how to do it yourself instead of just showing you how to do it.
THE SCRIPT:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
private float speed = 5f;
[SerializeField]
private float mouseSensitivity = 2f;
private Vector3 moveDirection;
private float rotationY;
void Update()
{
HandleMovement();
HandleRotation();
}
private void HandleMovement()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
moveDirection = new Vector3(horizontal, 0f, vertical).normalized;
transform.Translate(moveDirection * speed * Time.deltaTime);
}
private void HandleRotation()
{
float mouseX = Input.GetAxis("Mouse X");
rotationY += mouseX * mouseSensitivity;
transform.rotation = Quaternion.Euler(0f, rotationY, 0f);
}
}
My Other Video Explaining Methods: • C# Methods Explained Simply in 5 Minutes |...
#unitymovement #thirdpersonmovement #unitytutorial