PlayerController

 avatar
unknown
csharp
2 months ago
1.4 kB
3
Indexable
using UnityEngine;

namespace Player.FinallCharacterController
{
    public class PlayerController : MonoBehaviour
    {
        [SerializeField] private CharacterController _characterController;
        [SerializeField] private Camera _playerCamera;

        public float runAcceleration = 0.25f;
        public float runSpeed = 4f;

        private PlayerLocomotionInput _playerLocomotionInput;

        private void Awake()
        {
            _playerLocomotionInput = GetComponent<PlayerLocomotionInput>();
        }

        private void Update()
        {
            Vector3 cameraForwardXZ = new Vector3(_playerCamera.transform.forward.x, 0f, _playerCamera.transform.forward.z).normalized;
            Vector3 cameraRightXZ = new Vector3(_playerCamera.transform.right.x, 0f, _playerCamera.transform.right.z).normalized;
            Vector3 movementDirection = cameraRightXZ * _playerLocomotionInput.MovementInput.x + cameraForwardXZ * _playerLocomotionInput.MovementInput.y;

            Vector3 movementDelta = movementDirection * runAcceleration * Time.deltaTime;
            Vector3 newVelcotiy = _characterController.velocity + movementDelta;
            
            // Move character (Unity suggests using once per frame)
            _characterController.Move(newVelcotiy * Time.deltaTime);
        }
    }
}

Leave a Comment