diff --git a/Assets/Scripts/Entities/Character.cs b/Assets/Scripts/Entities/Character.cs
index 1e1b722..51f9e56 100644
--- a/Assets/Scripts/Entities/Character.cs
+++ b/Assets/Scripts/Entities/Character.cs
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using UnityEngine.Networking;
+using Cyber.Networking.Clientside;
namespace Cyber.Entities {
@@ -25,6 +26,8 @@ namespace Cyber.Entities {
public Transform Head;
private Vector3 MovementDirection = new Vector3();
+ private Vector3 ServerPosition = new Vector3();
+ private bool ServerPositionShouldLerpSync = false;
///
/// Moves the character in the given direction.
@@ -90,15 +93,24 @@ namespace Cyber.Entities {
///
///
public override void Deserialize(NetworkReader reader) {
- Vector3 ServerPosition = reader.ReadVector3();
+ ServerPosition = reader.ReadVector3();
Vector3 ServerMovementDirection = reader.ReadVector3();
Vector3 ServerRotation = reader.ReadVector3();
float Drift = (ServerPosition - transform.position).magnitude;
- if (Drift > MovementSpeed * 0.5f) {
+
+ // Update position if this is the local player
+ if (Drift > MovementSpeed * 0.5f && Client.GetConnectedPlayer().Character.Equals(this)) {
transform.position = ServerPosition;
MovementDirection = ServerMovementDirection;
}
+
+ // Update position more often (with lerping) if this is not the local player
+ if (Drift < 0.1) {
+ ServerPositionShouldLerpSync = false;
+ } else if (!Client.GetConnectedPlayer().Character.Equals(this)) {
+ ServerPositionShouldLerpSync = true;
+ }
}
///
@@ -111,6 +123,12 @@ namespace Cyber.Entities {
writer.Write(GetRotation());
}
+ private void Update() {
+ if (ServerPositionShouldLerpSync) {
+ transform.position = Vector3.Lerp(transform.position, ServerPosition, 10f * Time.deltaTime);
+ }
+ }
+
private void FixedUpdate() {
CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime);
}
diff --git a/Assets/Scripts/Networking/Clientside/CConnectedPlayer.cs b/Assets/Scripts/Networking/Clientside/CConnectedPlayer.cs
index 1c598b9..2d4a0dc 100644
--- a/Assets/Scripts/Networking/Clientside/CConnectedPlayer.cs
+++ b/Assets/Scripts/Networking/Clientside/CConnectedPlayer.cs
@@ -6,7 +6,7 @@ namespace Cyber.Networking.Clientside {
/// Represents a connected player on the clientside. This class is used by clients.
/// The C stands for "Client".
///
- class CConnectedPlayer {
+ public class CConnectedPlayer {
///
/// Connection ID on the global perspective.
diff --git a/Assets/Scripts/Networking/Clientside/Client.cs b/Assets/Scripts/Networking/Clientside/Client.cs
index f687d8b..374a124 100644
--- a/Assets/Scripts/Networking/Clientside/Client.cs
+++ b/Assets/Scripts/Networking/Clientside/Client.cs
@@ -91,6 +91,13 @@ namespace Cyber.Networking.Clientside {
return Singleton.NetClient.isConnected;
}
+ ///
+ /// Returns the connected player.
+ ///
+ /// The connected player.
+ public static CConnectedPlayer GetConnectedPlayer() {
+ return Singleton.Player;
+ }
private void Start() {
Spawner = GetComponent();