GuilhermeBed12

Untitled

Jun 27th, 2026
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3.  
  4. [RequireComponent(typeof(CharacterController))]
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     [Header("References")]
  8.     public Transform playerCamera;
  9.  
  10.     [Header("Movement Settings")]
  11.     public float walkSpeed = 5f;
  12.     public float sprintSpeed = 8f;
  13.     public float jumpHeight = 1.5f;
  14.  
  15.     [Header("Look Settings")]
  16.     public float mouseSensitivity = 0.15f;
  17.  
  18.     // Component and Input References
  19.     private CharacterController controller;
  20.     private PlayerInputActions inputActions;
  21.  
  22.     // Internal State
  23.     private Vector3 velocity;
  24.     private float cameraPitch = 0f;
  25.  
  26.     private void Awake()
  27.     {
  28.         controller = GetComponent<CharacterController>();
  29.         inputActions = new PlayerInputActions();
  30.  
  31.         // Subscribe to discrete actions (Jump)
  32.         inputActions.Player.Jump.performed += OnJump;
  33.     }
  34.  
  35.     private void OnEnable()
  36.     {
  37.         inputActions.Enable();
  38.         Cursor.lockState = CursorLockMode.Locked;
  39.         Cursor.visible = false;
  40.     }
  41.  
  42.     private void OnDisable()
  43.     {
  44.         inputActions.Disable();
  45.     }
  46.  
  47.     private void OnDestroy()
  48.     {
  49.         // Prevent memory leaks by unsubscribing
  50.         if (inputActions != null)
  51.         {
  52.             inputActions.Player.Jump.performed -= OnJump;
  53.             inputActions.Dispose();
  54.         }
  55.     }
  56.  
  57.     private void Update()
  58.     {
  59.         HandleLook();
  60.         HandleMovement();
  61.         ApplyGravity();
  62.     }
  63.  
  64.     private void HandleLook()
  65.     {
  66.         if (playerCamera == null)
  67.         {
  68.             Debug.LogError("[PlayerController] Player Camera is not assigned!");
  69.             return;
  70.         }
  71.  
  72.         // Poll delta directly rather than caching via events
  73.         Vector2 lookInput = inputActions.Player.Look.ReadValue<Vector2>();
  74.  
  75.         cameraPitch -= lookInput.y * mouseSensitivity;
  76.         cameraPitch = Mathf.Clamp(cameraPitch, -90f, 90f);
  77.  
  78.         playerCamera.localRotation = Quaternion.Euler(cameraPitch, 0f, 0f);
  79.         transform.Rotate(Vector3.up * (lookInput.x * mouseSensitivity));
  80.     }
  81.  
  82.     private void HandleMovement()
  83.     {
  84.         // Poll continuous movement input
  85.         Vector2 moveInput = inputActions.Player.Move.ReadValue<Vector2>();
  86.        
  87.         // Normalize to prevent faster diagonal movement
  88.         Vector3 moveDirection = (transform.right * moveInput.x + transform.forward * moveInput.y).normalized;
  89.  
  90.         bool isSprinting = inputActions.Player.Sprint.IsPressed();
  91.         float currentSpeed = isSprinting ? sprintSpeed : walkSpeed;
  92.  
  93.         controller.Move(moveDirection * (currentSpeed * Time.deltaTime));
  94.     }
  95.  
  96.     private void ApplyGravity()
  97.     {
  98.         // Reset gravity accumulation when grounded
  99.         if (controller.isGrounded && velocity.y < 0f)
  100.         {
  101.             velocity.y = -2f; // Slight downward force to snap to ground
  102.         }
  103.  
  104.         velocity.y += Physics.gravity.y * Time.deltaTime;
  105.         controller.Move(velocity * Time.deltaTime);
  106.     }
  107.  
  108.     private void OnJump(InputAction.CallbackContext context)
  109.     {
  110.         if (controller.isGrounded)
  111.         {
  112.             // Physics formula for jumping: v = sqrt(height * -2 * gravity)
  113.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * Physics.gravity.y);
  114.         }
  115.     }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment