Advertisement
ImAxel0

FirstPersonCharacter

Jun 12th, 2025
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | Gaming | 0 0
  1.     public class FirstPersonCharacter : MonoBehaviour
  2.     {
  3.         public static FirstPersonCharacter Instance { get; private set; }
  4.         public PlayerInputManager InputManager { get; private set; }
  5.  
  6.         [SerializeField]
  7.         private PlayerInventory _inventory;
  8.         public PlayerInventory Inventory => _inventory;
  9.  
  10.         [Header("Movement Settings")]
  11.         [SerializeField] private float _walkSpeed = 4f;
  12.         [SerializeField] private float _backwardWalkSpeed = 2f;
  13.         [SerializeField] private float _runSpeed = 7f;
  14.         [SerializeField] private float _backwardRunSpeed = 4f;
  15.         [SerializeField] private float _jumpForce = 5f;
  16.         [SerializeField] private float _airControlMultiplier = 0.4f;
  17.  
  18.         [Header("Ground Check Settings")]
  19.         [SerializeField] private Transform _groundCheck;
  20.         [SerializeField] private float _groundDistance = 0.3f;
  21.         [SerializeField] private LayerMask _groundMask;
  22.  
  23.         [Header("Camera")]
  24.         [SerializeField] private Camera _playerCam;
  25.         [SerializeField] private Transform _camPositionTr;
  26.         public Transform CamPositionTr => _camPositionTr;
  27.  
  28.         private Vector2 _rawMoveInput;
  29.         private Vector3 _moveDirection;
  30.         private bool _jumpRequested;
  31.         private bool _isGrounded;
  32.  
  33.         [SerializeField] private Rigidbody _rb;
  34.         public Rigidbody Rb => _rb;
  35.  
  36.         private void Awake()
  37.         {
  38.             Instance = this;
  39.  
  40.             InputManager = GetComponent<PlayerInputManager>();
  41.  
  42.             if (_rb == null)
  43.                 _rb = GetComponent<Rigidbody>();
  44.  
  45.             _rb.freezeRotation = true;
  46.         }
  47.  
  48.         private void Update()
  49.         {
  50.             _rawMoveInput = InputManager.MoveInput;
  51.  
  52.             Vector3 moveInput = new Vector3(_rawMoveInput.x, 0, _rawMoveInput.y).normalized;
  53.             _moveDirection = transform.TransformDirection(moveInput);
  54.  
  55.             // Ground Check
  56.             _isGrounded = Physics.CheckSphere(_groundCheck.position, _groundDistance, _groundMask);
  57.  
  58.             if (InputManager.Jumped && _isGrounded && _rb.velocity.y <= 0)
  59.             {
  60.                 _jumpRequested = true;
  61.             }
  62.         }
  63.  
  64.         private void FixedUpdate()
  65.         {
  66.             // make camera follow the player
  67.             _playerCam.transform.position = _camPositionTr.position;
  68.  
  69.             HandleMovement();
  70.             HandleJump();
  71.         }
  72.  
  73.         private void HandleMovement()
  74.         {
  75.             float speed = InputManager.Running && _isGrounded ? _runSpeed : _walkSpeed;
  76.             if (InputManager.MoveBackward)
  77.             {
  78.                 speed = InputManager.Running && _isGrounded ? _backwardRunSpeed : _backwardWalkSpeed;
  79.             }
  80.  
  81.             if (_isGrounded)
  82.             {
  83.                 Vector3 targetVelocity = _moveDirection * speed;
  84.                 Vector3 velocityChange = targetVelocity - new Vector3(_rb.velocity.x, 0, _rb.velocity.z);
  85.                 _rb.AddForce(velocityChange, ForceMode.VelocityChange);
  86.             }
  87.             else
  88.             {
  89.                 Vector3 airVelocity = _airControlMultiplier * speed * _moveDirection;
  90.                 _rb.AddForce(airVelocity, ForceMode.Acceleration);
  91.             }
  92.         }
  93.  
  94.         private void HandleJump()
  95.         {
  96.             if (_jumpRequested)
  97.             {
  98.                 _rb.velocity = new Vector3(_rb.velocity.x, 0f, _rb.velocity.z); // Reset vertical velocity
  99.                 _rb.AddForce(Vector3.up * _jumpForce, ForceMode.Impulse);
  100.                 _jumpRequested = false;
  101.             }
  102.         }
  103.  
  104.         private void OnDrawGizmosSelected()
  105.         {
  106.             if (_groundCheck == null) return;
  107.             Gizmos.color = Color.green;
  108.             Gizmos.DrawWireSphere(_groundCheck.position, _groundDistance);
  109.         }
  110.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement