Advertisement
Krythic

PlayerController

Jun 8th, 2024
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour
  4. {
  5.     public float moveSpeed = 5f;
  6.     public float rotateSpeed = 150f;
  7.     public float gravity = -9.81f;
  8.  
  9.     private CharacterController _characterController;
  10.     private Vector3 _velocity;
  11.     public Animator animator;
  12.     private bool _isWalking;
  13.  
  14.     public Camera playerCamera;
  15.     public float cameraDistance = 5.0f; // Distance the camera stays behind the player
  16.     public float cameraHeight = 2.0f; // Height the camera stays above the player
  17.     public float cameraRotationSpeed = 5.0f; // Speed of camera rotation around the player
  18.  
  19.     private float _currentCameraAngle = 0f; // Current angle of the camera around the player
  20.     private Collider _playerCollider;
  21.  
  22.     private void Start()
  23.     {
  24.         _characterController = GetComponent<CharacterController>();
  25.         _playerCollider = GetComponent<Collider>(); // Get the Collider component
  26.     }
  27.  
  28.     private void Update()
  29.     {
  30.         HandleMovement();
  31.         ApplyGravity();
  32.         HandleCamera();
  33.     }
  34.  
  35.     private void HandleMovement()
  36.     {
  37.         float moveDirection = 0f;
  38.         _isWalking = false;
  39.         if (Input.GetKey(KeyCode.W))
  40.         {
  41.             _isWalking = true;
  42.             moveDirection = 1f;
  43.         }
  44.         else if (Input.GetKey(KeyCode.S))
  45.         {
  46.             _isWalking = true;
  47.             moveDirection = -1f;
  48.         }
  49.         Vector3 cameraForward = playerCamera.transform.forward;
  50.         cameraForward.y = 0f; // Ensure the direction is only horizontal
  51.         Vector3 move = cameraForward * moveDirection * moveSpeed * Time.deltaTime;
  52.         Vector3 strafe = Vector3.zero;
  53.         if (Input.GetKey(KeyCode.A))
  54.         {
  55.             _isWalking = true;
  56.             strafe -= playerCamera.transform.right;
  57.         }
  58.         else if (Input.GetKey(KeyCode.D))
  59.         {
  60.             _isWalking = true;
  61.             strafe += playerCamera.transform.right;
  62.         }
  63.         strafe *= moveSpeed * Time.deltaTime;
  64.         Vector3 combinedMove = move + strafe;
  65.         _characterController.Move(combinedMove);
  66.         // Update the walking animation state
  67.         if (combinedMove.magnitude > 0)
  68.         {
  69.             _isWalking = true;
  70.             animator.SetBool("isRunning", true);
  71.         }
  72.         else
  73.         {
  74.             _isWalking = false;
  75.             animator.SetBool("isRunning", false);
  76.         }
  77.         // Calculate the target rotation based on the combined movement direction
  78.         if (combinedMove.magnitude > 0)
  79.         {
  80.             Quaternion targetRotation = Quaternion.LookRotation(combinedMove.normalized, Vector3.up);
  81.             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, 500 * Time.deltaTime);
  82.         }
  83.     }
  84.  
  85.     private void ApplyGravity()
  86.     {
  87.         if (_characterController.isGrounded && _velocity.y < 0)
  88.         {
  89.             _velocity.y = 0f;
  90.         }
  91.  
  92.         _velocity.y += gravity * Time.deltaTime;
  93.         _characterController.Move(_velocity * Time.deltaTime);
  94.     }
  95.  
  96.     private void HandleCamera()
  97.     {
  98.         if (Input.GetMouseButton(1))
  99.         {
  100.             float horizontal = Input.GetAxis("Mouse X") * cameraRotationSpeed;
  101.             _currentCameraAngle += horizontal;
  102.         }
  103.         Quaternion cameraRotation = Quaternion.Euler(0, _currentCameraAngle, 0);
  104.         Vector3 cameraPositionOffset = cameraRotation * new Vector3(0, 0, -cameraDistance);
  105.         playerCamera.transform.position = transform.position + cameraPositionOffset + Vector3.up * cameraHeight;
  106.         Vector3 lookAtPoint = CalculateLookAtPoint();
  107.         playerCamera.transform.LookAt(lookAtPoint);
  108.     }
  109.  
  110.     private Vector3 CalculateLookAtPoint()
  111.     {
  112.         if (_playerCollider != null)
  113.         {
  114.             Bounds bounds = _playerCollider.bounds;
  115.             return bounds.center + Vector3.up * (bounds.extents.y * 0.75f); // Look at a point 3/4th up from the base of the player
  116.         }
  117.         else
  118.         {
  119.             return transform.position + Vector3.up * (cameraHeight / 4); // Fallback in case there is no collider
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement