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

A LagCompensator Revamp #3770

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Added UpdateTargetCollider() and DisableColldiers(). These would be u…
…sed if you want greater perf at the cost of more manual scripting.
Amph1dian committed Feb 26, 2024
commit 1a05edfd5a58322ab4316986090fa3806448799f
51 changes: 51 additions & 0 deletions Assets/Mirror/Components/LagCompensation/LagCompensator.cs
Original file line number Diff line number Diff line change
@@ -197,5 +197,56 @@ public async void UpdateColliders(NetworkConnectionToClient localCon = null)
});
await Task.WhenAll(tasks);
}

[Server]
// Your going to want to await this method.
public void UpdateTargetCollider(NetworkConnectionToClient targetcon,
NetworkConnectionToClient localCon = null)
{
// never trust the client: estimate client time instead.
double buffertime = (NetworkManager.singleton.sendRate / 100) * 9;
double rtt = localCon.rtt; // the function needs rtt, which is latency * 2
double estimatedTime = LagCompensation.EstimateTime(NetworkTime.localTime, rtt, buffertime);

LagCompensator conPlayer = targetcon.identity.GetComponent<LagCompensator>();
Capture3D capture = new Capture3D();

// sample the history to get the nearest snapshots around 'timestamp'
if (LagCompensation.Sample(conPlayer.history, estimatedTime, lagCompensationSettings.captureInterval, out resultBefore, out resultAfter, out double t))
{
// interpolate to get a decent estimation at exactly 'timestamp'
capture = Capture3D.Interpolate(resultBefore, resultAfter, t);
resultTime = NetworkTime.localTime;
}
else Debug.Log($"CmdClicked: history doesn't contain {estimatedTime:F3}, netcon: {targetcon}, history: {conPlayer.history.Count}");

if(!conPlayer.compensatedGameObject.activeInHierarchy)
conPlayer.compensatedGameObject.SetActive(true);

conPlayer.compensatedPosition.position = capture.position;
conPlayer.compensatedOrientation.rotation = capture.rotation;

// Animation Compensation
if (useAnimator)
{
for (int i = 0; i < capture.animparam.Length; i++)
{
conPlayer.compensatedAnimator.Play(capture.animparam[i].animname, capture.animparam[i].layer, capture.animparam[i].time);
}
// NOTE: Doesnt set the variables of BLEND TREES. you will have to set the blend tree variables manually on the server.
}
}

[Server]
// To be used after you have shot your Raycast. only needed if using UpdateTargetCollider. UpdateColliders automatically does this.
public async void DisableColliders()
{
var tasks = NetworkServer.connections.Values.Select(async netcon =>
{
LagCompensator conPlayer = netcon.identity.GetComponent<LagCompensator>();
conPlayer.gameObject.SetActive(false);
});
await Task.WhenAll(tasks);
}
}
}