Advertisement
leomovskii

PlayerController

Mar 15th, 2025
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerController : MonoBehaviour {
  4.  
  5.     private static PlayerController _me;
  6.     public static PlayerController Me {
  7.         get {
  8.             if (_me == null)
  9.                 _me = GameObject.FindObjectOfType<PlayerController>();
  10.             return _me;
  11.         }
  12.     }
  13.  
  14.     public float speed;
  15.     public float jumpHeight;
  16.     public float gravity = 20f;
  17.  
  18.     [Space]
  19.  
  20.     public Transform cameraTransform;
  21.     public float horizontalSensitivity = 1f;
  22.     public float verticalSensitivity = 1f;
  23.     public float pitchMin = -70f;
  24.     public float pitchMax = 80f;
  25.  
  26.     [Space]
  27.  
  28.     public LayerMask playerLayer;
  29.     public Transform aimPoint;
  30.  
  31.     private CharacterController _controller;
  32.     private Health _health;
  33.     private Vector3 _input;
  34.     private Vector3 _verticalVelocity;
  35.     private Vector3 _yawVector;
  36.     private Vector3 _currentPitch;
  37.  
  38.     private void Start() {
  39.         _me = this;
  40.  
  41.         Cursor.lockState = CursorLockMode.Locked;
  42.         Cursor.visible = false;
  43.  
  44.         _controller = GetComponent<CharacterController>();
  45.         _currentPitch = cameraTransform.localEulerAngles;
  46.  
  47.         _health = GetComponent<Health>();
  48.         _health.OnDeathEvent.AddListener(Endgame);
  49.     }
  50.  
  51.     private void Update() {
  52.         MoveUpdate();
  53.         LookUpdate();
  54.     }
  55.  
  56.     private void MoveUpdate() {
  57.         bool isGrounded = _controller.isGrounded;
  58.  
  59.         _input.x = Input.GetAxisRaw("Horizontal");
  60.         _input.z = Input.GetAxisRaw("Vertical");
  61.         _input.Normalize();
  62.  
  63.         if (_verticalVelocity.y < -2f && isGrounded)
  64.             _verticalVelocity.y = -1f;
  65.  
  66.         Vector3 moveVector = transform.forward * _input.z + transform.right * _input.x;
  67.         _controller.Move(speed * Time.deltaTime * moveVector);
  68.  
  69.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded) {
  70.             _verticalVelocity.y = Mathf.Sqrt(jumpHeight * 2f * gravity);
  71.         }
  72.  
  73.         _verticalVelocity.y -= gravity * Time.deltaTime;
  74.         _controller.Move(_verticalVelocity * Time.deltaTime);
  75.     }
  76.  
  77.     private void LookUpdate() {
  78.         _yawVector.y = Input.GetAxis("Mouse X") * horizontalSensitivity;
  79.         float pitch = Input.GetAxis("Mouse Y") * verticalSensitivity;
  80.  
  81.         transform.Rotate(_yawVector);
  82.  
  83.         _currentPitch.x = Mathf.Clamp(_currentPitch.x - pitch, pitchMin, pitchMax);
  84.         cameraTransform.localEulerAngles = _currentPitch;
  85.     }
  86.  
  87.     private void Endgame() {
  88.         Debug.Log("Lose");
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement