Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft - Update, Late, Fixed. #3856

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace Mirror
{
public enum CoordinateSpace { Local, World }
public enum UpdateMethod { Update, LateUpdate, FixedUpdate }

public abstract class NetworkTransformBase : NetworkBehaviour
{
Expand Down Expand Up @@ -92,6 +93,11 @@ public abstract class NetworkTransformBase : NetworkBehaviour
protected double timeStampAdjustment => NetworkServer.sendInterval * (sendIntervalMultiplier - 1);
protected double offset => timelineOffset ? NetworkServer.sendInterval * sendIntervalMultiplier : 0;

// Update Method ///////////////////////////////////////////////////////////
[Header("Update Method")]
[Tooltip("Update by default. Try a different method when having problems with Physics or Animations.")]
public UpdateMethod updateMethod = UpdateMethod.Update;

// debugging ///////////////////////////////////////////////////////////
[Header("Debug")]
public bool showGizmos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public class NetworkTransformReliable : NetworkTransformBase
// update //////////////////////////////////////////////////////////////
void Update()
{
// if server then always sync to others.
if (isServer) UpdateServer();
// 'else if' because host mode shouldn't send anything to server.
// it is the server. don't overwrite anything there.
else if (isClient) UpdateClient();
if (updateMethod == UpdateMethod.Update)
UpdateCall();
}

void LateUpdate()
{
if (updateMethod == UpdateMethod.LateUpdate)
UpdateCall();

// set dirty to trigger OnSerialize. either always, or only if changed.
// It has to be checked in LateUpdate() for onlySyncOnChange to avoid
// the possibility of Update() running first before the object's movement
Expand All @@ -70,6 +70,21 @@ void LateUpdate()
}
}

void FixedUpdate()
{
if (updateMethod == UpdateMethod.FixedUpdate)
UpdateCall();
}

void UpdateCall()
{
// if server then always sync to others.
if (isServer) UpdateServer();
// 'else if' because host mode shouldn't send anything to server.
// it is the server. don't overwrite anything there.
else if (isClient) UpdateClient();
}

protected virtual void UpdateServer()
{
// apply buffered snapshots IF client authority
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public class NetworkTransformUnreliable : NetworkTransformBase
// Update applies interpolation
void Update()
{
if (isServer) UpdateServerInterpolation();
// for all other clients (and for local player if !authority),
// we need to apply snapshots from the buffer.
// 'else if' because host mode shouldn't interpolate client
else if (isClient && !IsClientWithAuthority) UpdateClientInterpolation();
if (updateMethod == UpdateMethod.Update)
UpdateCall();
}

// LateUpdate broadcasts.
Expand All @@ -50,6 +47,9 @@ void Update()
// this could cause visible jitter.
void LateUpdate()
{
if (updateMethod == UpdateMethod.LateUpdate)
UpdateCall();

// if server then always sync to others.
if (isServer) UpdateServerBroadcast();
// client authority, and local player (= allowed to move myself)?
Expand All @@ -58,6 +58,21 @@ void LateUpdate()
else if (isClient && IsClientWithAuthority) UpdateClientBroadcast();
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

This code was moved to UpdateCall

void FixedUpdate()
{
if (updateMethod == UpdateMethod.FixedUpdate)
UpdateCall();
}
Comment on lines +61 to +65
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to Base


void UpdateCall()
{
if (isServer) UpdateServerInterpolation();
// for all other clients (and for local player if !authority),
// we need to apply snapshots from the buffer.
// 'else if' because host mode shouldn't interpolate client
else if (isClient && !IsClientWithAuthority) UpdateClientInterpolation();
}

protected virtual void CheckLastSendTime()
{
// We check interval every frame, and then send if interval is reached.
Expand Down