实现移动平台乘客接口,优化乘客跟随逻辑

This commit is contained in:
2026-05-21 17:09:06 +08:00
parent fcd3e2dcdd
commit 247a218182
5 changed files with 122 additions and 31 deletions

View File

@@ -11,7 +11,7 @@ namespace BaseGames.Player
// 开头能在状态机写入速度之前先应用"强制清零"标记。
[DefaultExecutionOrder(-200)]
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerMovement : MonoBehaviour
public class PlayerMovement : MonoBehaviour, IPassengerReceiver
{
[Header("配置")]
[SerializeField] private PlayerMovementConfigSO _config;
@@ -31,6 +31,7 @@ namespace BaseGames.Player
// 下一个 FixedUpdate-200先于状态机 -100读取并清零
// 防止状态机用旧输入把速度重新写成非零值。
private bool _pendingHorizontalZero;
private Vector2 _pendingPlatformDelta; // MovingPlatform 推送的本帧位移,在 FixedUpdate 最前端消费
private bool _isWallLeft;
private bool _isWallRight;
private bool _onOneWayPlatform;
@@ -83,6 +84,13 @@ namespace BaseGames.Player
private void FixedUpdate()
{
// 消费移动平台推送的位移(在 velocity 系统之前直接写 position避免冲突
if (_pendingPlatformDelta != Vector2.zero)
{
_rb.position += _pendingPlatformDelta;
_pendingPlatformDelta = Vector2.zero;
}
// 优先处理来自 Update 的强制清零请求(在状态机 OnStateFixedUpdate 之前执行)。
if (_pendingHorizontalZero)
{
@@ -221,6 +229,17 @@ namespace BaseGames.Player
/// <summary>消耗墙壁土狼时间,防止同一帧被多次触发。</summary>
public void ConsumeWallCoyote() => _wallCoyoteTimer = 0f;
// ── IPassengerReceiver ────────────────────────────────────────────────
/// <summary>
/// MovingPlatform.FixedUpdate(-300) 推送本帧平台位移。
/// 在本类 FixedUpdate(-200) 最前端消费,直接写 _rb.position
/// 与 velocity 系统完全隔离,避免 Dynamic Rigidbody 上 MovePosition + velocity 叠加。
/// </summary>
public void SetPlatformDelta(Vector2 delta) => _pendingPlatformDelta += delta;
/// <summary>离开移动平台时继承平台速度,防止速度骤变卡顿。</summary>
public void OnLeavePlatform(Vector2 platformVelocity) => _rb.velocity += platformVelocity;
// ── 冲刺 ──────────────────────────────────────────────────────────────
/// <summary>
/// 施加冲刺速度DashState/DashState 调用)。