This commit is contained in:
Alexandre Coirier
2026-03-17 23:39:01 +01:00
parent 758a1d420c
commit 7058396d8e
5 changed files with 10477 additions and 23 deletions

View File

@@ -43,6 +43,11 @@ public class Player : MonoBehaviour
[Tooltip("Layers considered as ground")]
public LayerMask GroundLayer = 1;
[Header("Forces")]
[Tooltip("Decay rate of extra forces (m/s²)")]
public float ExtraForcesDrag = 8f;
[Header("Debug")]
[Tooltip("GUI logs of current state")]
@@ -102,6 +107,11 @@ public class Player : MonoBehaviour
private Vector3 _lastPlatformPosition;
private Quaternion _lastPlatformRotation;
// External forces
private Vector3 _impulseForce; // AddForce: decays automatically each frame
private Vector3 _persistentForce; // SetForce: maintained by caller
private Vector3 _platformVelocity; // Inherited on leaving platform: decays automatically
// Events
public event Action<PlayerState, PlayerState> OnStateChanged;
#endregion
@@ -164,7 +174,6 @@ public class Player : MonoBehaviour
_references.Controller.stepOffset = 0.4f;
CharacterController cc = _references.Controller;
_groundCheckRayOffset = cc.center + Vector3.up * (-cc.height * .5f - cc.skinWidth + _settings.GroundTolerance);
_groundCheckSphereOffset = cc.center + Vector3.up * (-cc.height * .5f + cc.radius - cc.skinWidth - _settings.GroundTolerance);
_groundCheckRadius = cc.radius;
@@ -198,7 +207,28 @@ public class Player : MonoBehaviour
#endregion
#region Physics
/// <summary>
/// Applies a one-shot impulse in any direction.
/// Decays automatically at ExtraForcesDrag rate.
/// Suitable for trampolines, explosions, knockbacks.
/// </summary>
public void AddForce(Vector3 force)
{
_impulseForce += force;
}
/// <summary>
/// Sets a persistent force applied every frame until cleared.
/// Caller is responsible for resetting to Vector3.zero.
/// Suitable for conveyor belts, jetpacks, wind zones.
/// </summary>
public void SetForce(Vector3 force)
{
_persistentForce = force;
if (force.y > 0)
_state.Velocity.y = 0;
}
#endregion
#region Public Methods
@@ -250,13 +280,16 @@ public class Player : MonoBehaviour
{
// Raycast for center contact
Vector3 rayOrigin = transform.position + _groundCheckRayOffset;
bool rayHit = Physics.Raycast(rayOrigin, Vector3.down, out RaycastHit rayInfo, _settings.GroundTolerance * 2f, _settings.GroundLayer);
bool rayHit = Physics.Raycast(rayOrigin, Vector3.down, out RaycastHit rayInfo,
_settings.GroundTolerance * 2f, _settings.GroundLayer);
// OverlapSphere for edge contact
// OverlapSphere for edge contact — works even when already intersecting
Vector3 sphereOrigin = transform.position + _groundCheckSphereOffset;
int overlapCount = Physics.OverlapSphereNonAlloc(sphereOrigin, _groundCheckRadius, _overlapResults, _settings.GroundLayer);
int overlapCount = Physics.OverlapSphereNonAlloc(sphereOrigin, _groundCheckRadius,
_overlapResults, _settings.GroundLayer);
bool sphereHit = overlapCount > 0;
bool wasGrounded = _state.IsGrounded;
_state.IsGrounded = rayHit || sphereHit;
if (_state.IsGrounded)
@@ -285,18 +318,23 @@ public class Player : MonoBehaviour
}
// Translation delta
transform.position += _state.Ground.position - _lastPlatformPosition;
Vector3 platformDelta = _state.Ground.position - _lastPlatformPosition;
transform.position += platformDelta;
// Store current state for next frame
_lastPlatformPosition = _state.Ground.position;
_lastPlatformRotation = _state.Ground.rotation;
// Sync physics broadphase to the new transform, the CharacterController
// doesn't see a stale overlap and generate a corrective push
// Sync physics broadphase — prevents CC from seeing stale overlap
Physics.SyncTransforms();
}
else
{
// Just left the ground: inherit platform velocity so the player
// doesn't stop dead in the air when jumping off a moving platform
if (wasGrounded && _state.Ground != null)
_platformVelocity = (_state.Ground.position - _lastPlatformPosition) / Time.deltaTime;
_state.Ground = null;
}
}
@@ -305,17 +343,36 @@ public class Player : MonoBehaviour
{
if (_state.IsGrounded && _state.Velocity.y < 0)
_state.Velocity.y = STICK_FORCE;
else
else if (_persistentForce.y <= 0)
_state.Velocity.y = Mathf.Max(_state.Velocity.y + GRAVITY * deltaTime, MAX_GRAVITY);
}
private void SetVelocity(float deltaTime)
{
// Movement attenuation based on state
float moveAtten;
switch (_state.CurrentState)
{
case PlayerState.Idle:
case PlayerState.Moving:
moveAtten = 1f;
break;
case PlayerState.Jumping:
moveAtten = .8f;
break;
case PlayerState.Falling:
moveAtten = .6f;
break;
default:
moveAtten = 0f;
break;
}
Vector2 input = _moveAction.ReadValue<Vector2>();
Vector3 forward = Vector3.ProjectOnPlane(_camera.transform.forward, Vector3.up).normalized;
Vector3 right = Vector3.ProjectOnPlane(_camera.transform.right, Vector3.up).normalized;
float speed = _settings.Speed * KMH_TO_MS;
float speed = _settings.Speed * KMH_TO_MS * moveAtten;
Vector3 moveInput = (forward * input.y + right * input.x) * speed;
_state.Velocity.x = moveInput.x;
@@ -341,7 +398,19 @@ public class Player : MonoBehaviour
private void SetMovement(float deltaTime)
{
_references.Controller.Move(_state.Velocity * deltaTime);
Vector3 totalVelocity = _state.Velocity + _impulseForce + _persistentForce + _platformVelocity;
_references.Controller.Move(totalVelocity * deltaTime);
// Decay impulse force
_impulseForce = Vector3.MoveTowards(_impulseForce, Vector3.zero,
_settings.ExtraForcesDrag * deltaTime);
// Decay inherited platform velocity — reset when back on ground
if (_state.IsGrounded)
_platformVelocity = Vector3.zero;
else
_platformVelocity = Vector3.MoveTowards(_platformVelocity, Vector3.zero,
_settings.ExtraForcesDrag * deltaTime);
}
private void SetState()