Sync and lerp rotation from server on the client side

This commit is contained in:
excitedneon 2017-05-11 22:19:37 +03:00
parent 710ef8d6b4
commit fa6109e9ac

View File

@ -33,6 +33,8 @@ namespace Cyber.Entities.SyncBases {
private Vector3 MovementDirection = new Vector3(); private Vector3 MovementDirection = new Vector3();
private Vector3 ServerPosition = new Vector3(); private Vector3 ServerPosition = new Vector3();
private bool ServerPositionShouldLerpSync = false; private bool ServerPositionShouldLerpSync = false;
private Vector3 ServerRotation = new Vector3();
private bool ServerRotationShouldLerpSync = false;
/// <summary> /// <summary>
/// Moves the character in the given direction. /// Moves the character in the given direction.
@ -124,7 +126,7 @@ namespace Cyber.Entities.SyncBases {
public override void Deserialize(NetworkReader reader) { public override void Deserialize(NetworkReader reader) {
ServerPosition = reader.ReadVector3(); ServerPosition = reader.ReadVector3();
Vector3 ServerMovementDirection = reader.ReadVector3(); Vector3 ServerMovementDirection = reader.ReadVector3();
Vector3 ServerRotation = reader.ReadVector3(); ServerRotation = reader.ReadVector3();
float Drift = (ServerPosition - GetPosition()).magnitude; float Drift = (ServerPosition - GetPosition()).magnitude;
@ -135,12 +137,10 @@ namespace Cyber.Entities.SyncBases {
MovementDirection = ServerMovementDirection; MovementDirection = ServerMovementDirection;
} }
// Update position more often (with lerping) if this is not the local player // Update position and rotation more often (with lerping)
if (Drift < 0.1) { // if this is not the local player
ServerPositionShouldLerpSync = false; ServerRotationShouldLerpSync = !LocalCharacter.Equals(this);
} else if (!LocalCharacter.Equals(this)) { ServerPositionShouldLerpSync = !LocalCharacter.Equals(this) && Drift > 0.1;
ServerPositionShouldLerpSync = true;
}
} }
/// <summary> /// <summary>
@ -157,6 +157,13 @@ namespace Cyber.Entities.SyncBases {
if (ServerPositionShouldLerpSync) { if (ServerPositionShouldLerpSync) {
SetPosition(Vector3.Lerp(GetPosition(), ServerPosition, 10f * Time.deltaTime)); SetPosition(Vector3.Lerp(GetPosition(), ServerPosition, 10f * Time.deltaTime));
} }
if (ServerRotationShouldLerpSync) {
Quaternion ServerRot = Quaternion.Euler(ServerRotation);
Quaternion LocalRot = Quaternion.Euler(GetRotation());
Vector3 NewRot = Quaternion.Lerp(LocalRot, ServerRot,
10f * Time.deltaTime).eulerAngles;
SetRotation(NewRot);
}
} }
private void FixedUpdate() { private void FixedUpdate() {