diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index 6ada261..ce3d1b9 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -492,7 +492,7 @@ MonoBehaviour: _speed: 2 _rigid: {fileID: -1362768914916555191} _inputComponent: {fileID: 8938569698484985372} - _applyServerCorrection: 0 + _applyServerCorrection: 1 _lerpRate: 0.1 --- !u!114 &8938569698484985372 MonoBehaviour: @@ -508,7 +508,6 @@ MonoBehaviour: m_EditorClassIdentifier: _playerId: 1234 _sendInterval: 0.05 - _useNetwork: 1 --- !u!54 &-1362768914916555191 Rigidbody: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/MovementComponent.cs b/Assets/Scripts/MovementComponent.cs index 72261f4..1f097b4 100644 --- a/Assets/Scripts/MovementComponent.cs +++ b/Assets/Scripts/MovementComponent.cs @@ -126,6 +126,17 @@ public class MovementComponent : MonoBehaviour _simulationAccumulator += Time.fixedDeltaTime; while (_simulationAccumulator >= kServerSimulationStepSeconds) { + var pendingCount = _predictionBuffer.PendingInputs.Count; + if (pendingCount == 0) + { + // 没有待处理的输入,清零累积时间,跳出循环 + _simulationAccumulator = 0f; + break; + } + + Debug.Log( + $"[SimulateLoop] frame={Time.frameCount} accum={_simulationAccumulator:F4} pendingCount={pendingCount}"); + // 使用最近发送的 MoveInput(来自 predictionBuffer)而非实时输入, // 确保客户端与服务端的输入时序一致 Simulate(GetLatestPredictedInput()); @@ -209,8 +220,12 @@ public class MovementComponent : MonoBehaviour private void Simulate(Vector3 input) { + Debug.Log( + $"[Simulate] frame={Time.frameCount} input=({input.x:F2},{input.z:F2}) accum={_simulationAccumulator:F4}"); ApplyTankMovement(-input.x, input.z, kServerSimulationStepSeconds); + //ApplyTankMovement(-input.x, input.z, kServerSimulationStepSeconds); + // 每次 Simulate 后累加模拟时间(用于 Reconcile 时的重放) _predictionBuffer.AccumulateLatest(kServerSimulationStepSeconds); @@ -344,8 +359,10 @@ public class MovementComponent : MonoBehaviour var forward = ResolveHeadingForward(heading); var velocity = forward * (clampedThrottleInput * _speed); - _rigid.velocity = velocity; - _rigid.position += velocity * deltaTime; + Vector3 targetPos = _rigid.position + velocity * deltaTime; + _rigid.MovePosition(targetPos); + // _rigid.velocity = velocity; + // _rigid.position += velocity * deltaTime; // 调试日志:打印每步计算细节 Debug.Log($"[MoveStep] _speed={_speed} deltaTime={deltaTime:F4} throttle={clampedThrottleInput} " +