11using System ;
22using Mirror ;
33using UnityEngine ;
4+ using UnityEngine . Serialization ;
45
56namespace Code . Network . Simulation
67{
@@ -11,27 +12,38 @@ struct RigidbodySnapshot
1112 public Vector3 angularVelocity ;
1213 public Quaternion rotation ;
1314 }
14-
15+
1516 /**
1617 * This component is used to allow for lag compensation to operate on rigidbodies controlled by the server.
1718 */
18- public class AirshipLagCompensatedEntity : NetworkBehaviour
19+ public class AirshipLagCompensatedRigidbody : NetworkBehaviour
1920 {
20- [ Tooltip ( "Time in seconds kept in history." ) ]
21- [ Range ( 0 , 1 ) ]
21+ [ Tooltip ( "Time kept in history in seconds in seconds." ) ] [ Range ( 0 , 1 ) ]
2222 public float historySize = 1 ;
2323
24+ [ Tooltip (
25+ "Amount of ticks buffered on the client before rendering. " +
26+ "It's important to get this number correct as it controls the additional rollback time required to correctly " +
27+ "produce the client's view of the world during lag compensation. It should match the number of physics updates " +
28+ "you wait before rendering the object. By default, Airship has 2 updates buffered by Mirror." ) ]
29+ public uint bufferSizeMultiplier = 2 ;
30+
31+ [ Tooltip ( "The send rate being used to update this rigidbodies positions. This is used to calculate the " +
32+ "additional rollback time required to produce the client's view of the world during lag compensation. " +
33+ "It should match the number of updates sent per second. Airship uses a default send rate of 50 for Mirror." ) ]
34+ public uint updateSendRate = 50 ;
35+
2436 private Rigidbody rb ;
2537 private History < RigidbodySnapshot > history ;
26-
38+
2739 private void Awake ( )
2840 {
2941 // Lag compensation only occurs on the server, so this component only operates on the server.
3042 if ( isServer )
3143 {
3244 rb = this . GetComponent < Rigidbody > ( ) ;
3345 var snapshots = historySize / Time . fixedDeltaTime ;
34- history = new History < RigidbodySnapshot > ( ( int ) Math . Ceiling ( snapshots ) ) ;
46+ history = new History < RigidbodySnapshot > ( ( int ) Math . Ceiling ( snapshots ) ) ;
3547 AirshipSimulationManager . OnCaptureSnapshot += this . CaptureSnapshot ;
3648 AirshipSimulationManager . OnSetSnapshot += this . SetSnapshot ;
3749 AirshipSimulationManager . OnLagCompensationCheck += this . LagCompensationCheck ;
@@ -71,8 +83,8 @@ private void SetSnapshot(double time)
7183
7284 private void LagCompensationCheck ( int clientId , double time , double latency )
7385 {
74- // TODO: configure buffer time
75- var bufferedTime = time - latency - ( 3f / Time . fixedDeltaTime ) ;
86+ var sendRate = 1f / this . updateSendRate ;
87+ var bufferedTime = time - latency - ( this . bufferSizeMultiplier * sendRate ) ;
7688 this . SetSnapshot ( bufferedTime ) ;
7789 }
7890 }
0 commit comments