1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| [RequireComponent(typeof(PlayerInput), typeof(CharacterController))] public class PlayerController : MonoBehaviour { [SerializeField] private PlayerInput playerInput; private StateMachine playerStateMachine;
[Header("Movement")] [Tooltip("Horizontal speed")] [SerializeField] private float moveSpeed = 5f; [Tooltip("Rate of change for move speed")] [SerializeField] private float acceleration = 10f; [Tooltip("Max height to jump")] [SerializeField] private float jumpHeight = 1.25f;
[Tooltip("Custom gravity for player")] [SerializeField] private float gravity = -15f; [Tooltip("Time between jumps")] [SerializeField] private float jumpTimeout = 0.1f;
[SerializeField] private bool isGrounded = true; [SerializeField] private float groundedRadius = 0.5f; [SerializeField] private float groundedOffset = 0.15f; [SerializeField] private LayerMask groundLayers;
public CharacterController CharController => charController; public bool IsGrounded => isGrounded; public StateMachine PlayerStateMachine => playerStateMachine;
private CharacterController charController; private float targetSpeed; private float verticalVelocity; private float jumpCooldown;
private void Awake() { playerInput = GetComponent<PlayerInput>(); charController = GetComponent<CharacterController>();
playerStateMachine = new StateMachine(this); }
private void Start() { playerStateMachine.Initialize(playerStateMachine.idleState); }
private void Update() { playerStateMachine.Update(); }
private void LateUpdate() { CalculateVertical(); Move(); }
private void Move() { Vector3 inputVector = playerInput.InputVector;
if (inputVector == Vector3.zero) { targetSpeed = 0; }
float currentHorizontalSpeed = new Vector3(charController.velocity.x, 0.0f, charController.velocity.z).magnitude; float tolerance = 0.1f;
if (currentHorizontalSpeed < targetSpeed - tolerance || currentHorizontalSpeed > targetSpeed + tolerance) { targetSpeed = Mathf.Lerp(currentHorizontalSpeed, targetSpeed, Time.deltaTime * acceleration); targetSpeed = Mathf.Round(targetSpeed * 1000f) / 1000f; } else { targetSpeed = moveSpeed; }
charController.Move((inputVector.normalized * targetSpeed * Time.deltaTime) + new Vector3(0f, verticalVelocity, 0f) * Time.deltaTime); }
private void CalculateVertical() { if (isGrounded) { if (verticalVelocity < 0f) { verticalVelocity = -2f; }
if (playerInput.IsJumping && jumpCooldown <= 0f) { verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * gravity); }
if (jumpCooldown >= 0f) { jumpCooldown -= Time.deltaTime; } } else { jumpCooldown = jumpTimeout; playerInput.IsJumping = false; }
verticalVelocity += gravity * Time.deltaTime;
Vector3 spherePosition = new Vector3(transform.position.x, transform.position.y + groundedOffset, transform.position.z); isGrounded = Physics.CheckSphere(spherePosition, 0.5f, groundLayers, QueryTriggerInteraction.Ignore); }
private void OnDrawGizmosSelected() { Color transparentGreen = new Color(0.0f, 1.0f, 0.0f, 0.35f); Color transparentRed = new Color(1.0f, 0.0f, 0.0f, 0.35f);
Gizmos.color = isGrounded ? transparentGreen : transparentRed;
Gizmos.DrawSphere(new Vector3(transform.position.x, transform.position.y + groundedOffset, transform.position.z), groundedRadius); } }
|