add callbacks again

This commit is contained in:
mischa 2024-03-28 21:23:01 +08:00
parent c3ea03f679
commit 8c4e9bbcef

View File

@ -426,6 +426,7 @@ protected virtual bool IsMoving() =>
predictedRigidbody.velocity.sqrMagnitude >= motionSmoothingVelocityThresholdSqr || predictedRigidbody.velocity.sqrMagnitude >= motionSmoothingVelocityThresholdSqr ||
predictedRigidbody.angularVelocity.sqrMagnitude >= motionSmoothingAngularVelocityThresholdSqr; predictedRigidbody.angularVelocity.sqrMagnitude >= motionSmoothingAngularVelocityThresholdSqr;
// TODO maybe merge the IsMoving() checks & callbacks with UpdateState().
void UpdateGhosting() void UpdateGhosting()
{ {
// perf: enough to check ghosts every few frames. // perf: enough to check ghosts every few frames.
@ -469,10 +470,46 @@ void UpdateGhosting()
} }
} }
// when using Fast mode, we don't create any ghosts.
// but we still want to check IsMoving() in order to support the same
// user callbacks.
bool lastMoving = false;
void UpdateState()
{
// perf: enough to check ghosts every few frames.
// PredictionBenchmark: only checking every 4th frame: 770 => 800 FPS
if (Time.frameCount % checkGhostsEveryNthFrame != 0) return;
bool moving = IsMoving();
// started moving?
if (moving && !lastMoving)
{
OnBeginPrediction();
lastMoving = true;
}
// stopped moving?
else if (!moving && lastMoving)
{
// ensure a minimum time since starting to move, to avoid on/off/on effects.
if (NetworkTime.time >= motionSmoothingLastMovedTime + motionSmoothingTimeTolerance)
{
OnEndPrediction();
lastMoving = false;
}
}
}
void Update() void Update()
{ {
if (isServer) UpdateServer(); if (isServer) UpdateServer();
if (isClientOnly && mode == PredictionMode.Smooth) UpdateGhosting(); if (isClientOnly)
{
if (mode == PredictionMode.Smooth)
UpdateGhosting();
else if (mode == PredictionMode.Fast)
UpdateState();
}
} }
void LateUpdate() void LateUpdate()