fix: NetworkTransformReliable fix jitter (fixes: #3368) (#3410)

* ninja nt

* remove unused

* unused

* comments
This commit is contained in:
mischa 2023-03-12 16:12:27 +01:00 committed by GitHub
parent 25ebb0620b
commit 7137ace5fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,6 +44,7 @@ public class NetworkTransformReliable : NetworkTransformBase
protected TransformSnapshot last; protected TransformSnapshot last;
// update ////////////////////////////////////////////////////////////// // update //////////////////////////////////////////////////////////////
// Update applies interpolation.
void Update() void Update()
{ {
// if server then always sync to others. // if server then always sync to others.
@ -53,7 +54,22 @@ void Update()
else if (isClient) UpdateClient(); else if (isClient) UpdateClient();
} }
void UpdateServer() // LateUpdate sets dirty.
// movement scripts may change positions in Update.
// use LateUpdate to ensure changes are detected in the same frame.
// otherwise this may run before user update, delaying detection until next frame.
// this would cause visible jitter.
void LateUpdate()
{
// set dirty to trigger OnSerialize. either always, or only if changed.
if (isServer || (IsClientWithAuthority && NetworkClient.ready)) // is NetworkClient.ready even needed?
{
if (!onlySyncOnChange || Changed(Construct()))
SetDirty();
}
}
protected virtual void UpdateServer()
{ {
// apply buffered snapshots IF client authority // apply buffered snapshots IF client authority
// -> in server authority, server moves the object // -> in server authority, server moves the object
@ -83,35 +99,13 @@ void UpdateServer()
Apply(computed, to); Apply(computed, to);
} }
} }
// set dirty to trigger OnSerialize. either always, or only if changed.
// technically snapshot interpolation requires constant sending.
// however, with reliable it should be fine without constant sends.
//
// detect changes _after_ all changes were applied above.
if (!onlySyncOnChange || Changed(Construct()))
SetDirty();
} }
void UpdateClient() protected virtual void UpdateClient()
{ {
// client authority, and local player (= allowed to move myself)? // client authority, and local player (= allowed to move myself)?
if (IsClientWithAuthority) if (!IsClientWithAuthority)
{ {
// https://github.com/vis2k/Mirror/pull/2992/
if (!NetworkClient.ready) return;
// set dirty to trigger OnSerialize. either always, or only if changed.
// technically snapshot interpolation requires constant sending.
// however, with reliable it should be fine without constant sends.
if (!onlySyncOnChange || Changed(Construct()))
SetDirty();
}
// for all other clients (and for local player if !authority),
// we need to apply snapshots from the buffer
else
{
// only while we have snapshots // only while we have snapshots
if (clientSnapshots.Count > 0) if (clientSnapshots.Count > 0)
{ {