Advertisement
uurha

Untitled

Feb 12th, 2023 (edited)
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.94 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Core.Extensions;
  4. using Core.Managers;
  5. using Core.Managers.Interface;
  6. using CustomAttributes.Headers;
  7. using UnityEngine;
  8.  
  9. namespace PlayerSystem.Movement
  10. {
  11.     [RequireComponent(typeof(CharacterController))]
  12.     public class PlayerMovement : MonoBehaviour, ICrossEventSubscriber
  13.     {
  14.         [ReferencesHeader]
  15.         [SerializeField] private Camera playerCamera;
  16.  
  17.         [SettingsHeader]
  18.         [SerializeField] private float crouchSpeed = 2.5f;
  19.         [SerializeField] private float walkingSpeed = 7.5f;
  20.         [SerializeField] private float runningSpeed = 11.5f;
  21.  
  22.         [SerializeField] private float jumpSpeed = 8.0f;
  23.         [SerializeField] private float lookSpeed = 2.0f;
  24.         [SerializeField] private float lookXLimit = 45.0f;
  25.  
  26.         private CharacterController _characterController;
  27.         private Vector3 _moveDirection = Vector3.zero;
  28.         private const float _gravity = -9.81f;
  29.         private float _rotationX;
  30.         private bool _canMove = true;
  31.         private PlayerHeightState _currentState;
  32.  
  33.         private void Awake()
  34.         {
  35.             _characterController = GetComponent<CharacterController>();
  36.         }
  37.  
  38.         private void Start()
  39.         {
  40.             // Lock cursor
  41.             StateTools.ChangeCursorState(false);
  42.         }
  43.  
  44.         private bool CanJump()
  45.         {
  46.             return Input.GetButton("Jump") &&
  47.                    _canMove &&
  48.                    _characterController.isGrounded && _currentState == PlayerHeightState.Default;
  49.         }
  50.  
  51.         private void Update()
  52.         {
  53.             // We are grounded, so recalculate move direction based on axes
  54.             var forward = transform.TransformDirection(Vector3.forward);
  55.             var right = transform.TransformDirection(Vector3.right);
  56.  
  57.             // Press Left Shift to run
  58.             var curSpeedX = CurSpeed(Input.GetAxis("Vertical"));
  59.             var curSpeedY = CurSpeed(Input.GetAxis("Horizontal"));
  60.             var movementDirectionY = _moveDirection.y;
  61.  
  62.             _moveDirection = forward * curSpeedX + right * curSpeedY;
  63.  
  64.             _moveDirection.y = CanJump() ? jumpSpeed : movementDirectionY;
  65.  
  66.             // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
  67.             // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
  68.             // as an acceleration (ms^-2)
  69.             if (!_characterController.isGrounded) _moveDirection.y += _gravity * Time.deltaTime;
  70.  
  71.             // Move the controller
  72.             _characterController.Move(_moveDirection * Time.deltaTime);
  73.  
  74.             // Player and Camera rotation
  75.             if (!_canMove ||
  76.                 !(Time.timeScale > 0))
  77.                 return;
  78.             _rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
  79.             _rotationX = Mathf.Clamp(_rotationX, -lookXLimit, lookXLimit);
  80.             playerCamera.transform.localRotation = Quaternion.Euler(_rotationX, 0, 0);
  81.             transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
  82.         }
  83.  
  84.         private float GetMaxSpeed()
  85.         {
  86.             float currentMaxSpeed;
  87.             if (_currentState == PlayerHeightState.Default)
  88.                 currentMaxSpeed = Input.GetKey(KeyCode.LeftShift) ? runningSpeed : walkingSpeed;
  89.             else
  90.                 currentMaxSpeed = crouchSpeed;
  91.  
  92.             return currentMaxSpeed;
  93.         }
  94.  
  95.         private float CurSpeed(float value)
  96.         {
  97.             return _canMove ? GetMaxSpeed() * value : 0;
  98.         }
  99.  
  100.         private void PlayerHeightChanged(PlayerHeightState newState)
  101.         {
  102.             _currentState = newState;
  103.         }
  104.  
  105.         public IEnumerable<Delegate> GetSubscribers()
  106.         {
  107.             var list = new Delegate[] {(CrossEventTypes.PlayerHeightChangedDelegate) PlayerHeightChanged};
  108.  
  109.             return list;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement