refactor
This commit is contained in:
132
Runtime/FPPKit/Code/FPPLocomotion.cs
Normal file
132
Runtime/FPPKit/Code/FPPLocomotion.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using UnityEngine;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace RebootKit.FPPKit {
|
||||
public class FPPLocomotion : MonoBehaviour {
|
||||
[SerializeField] CharacterController m_CharacterController;
|
||||
|
||||
public float maxMovementSpeed = 4.0f;
|
||||
public float maxSprintSpeed = 15.0f;
|
||||
public float jumpHeight = 1.0f;
|
||||
public float gravity = 10f;
|
||||
public float maxFallSpeed = 20f;
|
||||
public float damping = 20.0f;
|
||||
float3 m_CurrentVelocity;
|
||||
bool m_IsFalling;
|
||||
|
||||
bool m_IsSprinting;
|
||||
bool m_JumpRequested;
|
||||
|
||||
float3 m_PendingInputValue;
|
||||
|
||||
public bool IsGrounded => m_CharacterController.isGrounded;
|
||||
|
||||
public float3 Velocity => m_CurrentVelocity;
|
||||
|
||||
void Update() {
|
||||
ConsumePendingInput();
|
||||
UpdateVerticalVelocity();
|
||||
|
||||
m_CharacterController.Move(m_CurrentVelocity * Time.deltaTime);
|
||||
|
||||
ApplyFriction();
|
||||
DetectFall();
|
||||
}
|
||||
|
||||
void DetectFall() {
|
||||
if (m_IsFalling && m_CharacterController.isGrounded) {
|
||||
m_IsFalling = false;
|
||||
} else if (!m_CharacterController.isGrounded) {
|
||||
m_IsFalling = true;
|
||||
}
|
||||
}
|
||||
|
||||
void ConsumePendingInput() {
|
||||
// if (!IsGrounded) {
|
||||
// m_PendingInputValue = float3.zero;
|
||||
// return;
|
||||
// }
|
||||
|
||||
m_PendingInputValue.y = 0.0f;
|
||||
|
||||
float pendingInputMagnitude = math.length(m_PendingInputValue);
|
||||
float3 direction = float3.zero;
|
||||
|
||||
if (pendingInputMagnitude > 0.0f) {
|
||||
direction = math.normalize(m_PendingInputValue);
|
||||
}
|
||||
|
||||
float movementSpeed = m_IsSprinting ? maxSprintSpeed : maxMovementSpeed;
|
||||
float3 movementVelocity = m_CurrentVelocity;
|
||||
movementVelocity.y = 0.0f;
|
||||
movementVelocity += direction * (movementSpeed * pendingInputMagnitude);
|
||||
movementVelocity.y = 0.0f;
|
||||
|
||||
float movementVelocityMagnitude = math.length(movementVelocity);
|
||||
float3 movementVelocityDirection = movementVelocityMagnitude > 0.0f ? math.normalize(movementVelocity) : float3.zero;
|
||||
|
||||
if (movementVelocityMagnitude > movementSpeed) {
|
||||
movementVelocityMagnitude = movementSpeed;
|
||||
}
|
||||
|
||||
movementVelocity = movementVelocityDirection * movementVelocityMagnitude;
|
||||
|
||||
m_CurrentVelocity.x = movementVelocity.x;
|
||||
m_CurrentVelocity.z = movementVelocity.z;
|
||||
|
||||
m_PendingInputValue = float3.zero;
|
||||
}
|
||||
|
||||
void UpdateVerticalVelocity() {
|
||||
if (m_CharacterController.isGrounded) {
|
||||
if (m_JumpRequested) {
|
||||
m_CurrentVelocity.y = math.sqrt(2.0f * gravity * jumpHeight);
|
||||
m_JumpRequested = false;
|
||||
} else {
|
||||
m_CurrentVelocity.y = -1f;
|
||||
}
|
||||
} else {
|
||||
m_CurrentVelocity.y -= gravity * Time.deltaTime;
|
||||
m_CurrentVelocity.y = math.max(m_CurrentVelocity.y, -maxFallSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
void ApplyFriction() {
|
||||
if (!IsGrounded) {
|
||||
return;
|
||||
}
|
||||
|
||||
float3 movementVelocity = m_CurrentVelocity;
|
||||
movementVelocity.y = 0.0f;
|
||||
|
||||
movementVelocity = math.lerp(movementVelocity, float3.zero, damping * Time.deltaTime);
|
||||
|
||||
m_CurrentVelocity.x = movementVelocity.x;
|
||||
m_CurrentVelocity.z = movementVelocity.z;
|
||||
}
|
||||
|
||||
public void AddVelocity(float3 velocity) {
|
||||
m_CurrentVelocity += velocity;
|
||||
}
|
||||
|
||||
public void AddMovementInput(float3 input, float scale) {
|
||||
m_PendingInputValue += input * scale;
|
||||
}
|
||||
|
||||
public void Jump() {
|
||||
if (!m_CharacterController.isGrounded) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_JumpRequested = true;
|
||||
}
|
||||
|
||||
public void StartSprint() {
|
||||
m_IsSprinting = true;
|
||||
}
|
||||
|
||||
public void StopSprint() {
|
||||
m_IsSprinting = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user