Skip to content

Commit 3d5ba57

Browse files
committed
Syncing position for observers via the CharacterMovement instead of the serialized predictive state
1 parent 8a97f7f commit 3d5ba57

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

Runtime/Code/Player/Character/MovementSystem/CharacterAnimationSyncData.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class CharacterAnimationSyncData {
99
public Vector3 localVelocity = Vector3.zero;
1010
public Vector3 lookVector = Vector3.zero;
1111

12+
//Just for observers in predicted mode
13+
public Vector3 position = Vector3.zero;
14+
1215
// override object.Equals
1316
public override bool Equals(object obj) {
1417
CharacterAnimationSyncData data = (CharacterAnimationSyncData)obj;

Runtime/Code/Player/Character/MovementSystem/CharacterMovement.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ private void Move(MoveInputData md) {
850850
crouching = isCrouching,
851851
localVelocity = currentLocalVelocity,
852852
lookVector = lookVector,
853+
position = transform.position,
853854
});
854855

855856
if (didJump){
@@ -1134,17 +1135,20 @@ private void CommandSetFlying(bool flyModeEnabled) {
11341135

11351136
private void TrySetState(CharacterAnimationSyncData newStateData) {
11361137
bool isNewState = newStateData.state != this.stateSyncData.state;
1137-
bool isNewData = !newStateData.Equals(this.stateSyncData);
1138+
bool isNewData = !newStateData.Equals(this.stateSyncData) || (isServerAuth && stateSyncData.position != newStateData.position);
11381139

11391140
// If new value in the state
1140-
if (isNewData) {
1141+
if (isNewData ) {
11411142
this.stateSyncData = newStateData;
11421143
if(hasMovementAuth){
11431144
if(isClientOnly){
11441145
CommandSetStateData(newStateData);
1145-
} else if (isServerOnly){
1146-
RpcSetStateData(newStateData);
11471146
}
1147+
// Right now the visual state is controlled by the client only
1148+
// We may want server auth to update the state but that makes observers have even older version of the character
1149+
// else if (isServerOnly ){
1150+
// RpcSetStateData(newStateData);
1151+
// }
11481152
}
11491153
}
11501154

@@ -1180,8 +1184,14 @@ private void ApplyNonLocalStateData(CharacterAnimationSyncData data) {
11801184
this.isGrounded = data.grounded;
11811185
this.isCrouching = data.crouching;
11821186
this.isSprinting = data.sprinting;
1187+
11831188
this.lookVector = data.lookVector;
11841189

1190+
//In server auth we don't sync position with the NetworkTransform so I am doing it here
1191+
if(this.IsObserver() && isServerAuth){
1192+
this.rigidbody.position = data.position;
1193+
}
1194+
11851195
if (oldState.state != data.state) {
11861196
stateChanged?.Invoke((int)data.state);
11871197
}

Runtime/Code/Player/Character/MovementSystem/ClientPrediction/AirshipPredictedController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ void ApplyState(T snapshotState){
319319
// serverTimestamp is the same value as serverState.timestamp
320320
protected void OnReceivedState(int serverTick, T serverState, bool forceReplay) {
321321
if(IsObserver()){
322-
SnapTo(serverState);
322+
//SnapTo(serverState);
323323
return;
324324
}
325325

Runtime/Code/Player/Character/MovementSystem/ClientPrediction/CharacterMovement/AirshipPredictedCharacterMovement.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
public class AirshipPredictedCharacterMovement : AirshipPredictedController<CharacterMovementState> {
1212

1313
#region PUBLIC
14-
[Header("References")]
14+
public bool pauseOnReplay = false;
15+
16+
[Header("Character References")]
1517
public CharacterMovement movement;
1618

1719
[Header("Variables")]
18-
public bool pauseOnReplay = false;
1920

2021
#endregion
2122

@@ -183,7 +184,15 @@ public override void SnapTo(CharacterMovementState newState){
183184
if(showLogs){
184185
print("Snapping Movement To: " + newState.tick);
185186
}
186-
movement.ForceToNewMoveState(newState);
187+
if(IsObserver()){
188+
//Observers just sync position. State and look vector are already synced by CharacterMovement
189+
movement.rigidbody.position = newState.position;
190+
movement.currentMoveState.position = newState.position;
191+
movement.currentMoveState.velocity = newState.velocity;
192+
}else{
193+
//Sync this state into the character movement
194+
movement.ForceToNewMoveState(newState);
195+
}
187196
}
188197

189198
public override void OnReplayStarted(AirshipPredictedState initialState, int historyIndex){

0 commit comments

Comments
 (0)