From e60ad420dc82b9e2a4fdf610bfa3554d3915fa87 Mon Sep 17 00:00:00 2001 From: SepComet <202308010230@stu.csust.edu.cn> Date: Tue, 7 Apr 2026 17:09:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E8=AF=A5=E8=A7=A3=E5=86=B3=E4=BA=86?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E9=97=AE=E9=A2=98=E6=9D=A5=E6=BA=90?= =?UTF-8?q?=EF=BC=9AAI=20=E5=9C=A8=E6=88=91=E4=B8=8D=E7=9F=A5=E6=83=85?= =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=E5=BC=95=E5=85=A5=E4=BA=86?= =?UTF-8?q?=E5=88=9A=E4=BD=93=E9=80=9F=E5=BA=A6=EF=BC=8C=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E4=B8=8E=E6=9C=8D=E5=8A=A1=E7=AB=AF?= =?UTF-8?q?=E7=9A=84=E7=8A=B6=E6=80=81=E4=B8=80=E7=9B=B4=E5=AF=B9=E4=B8=8D?= =?UTF-8?q?=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Prefabs/Player.prefab | 3 +-- Assets/Scripts/MovementComponent.cs | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) 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} " +