Make non-controlled characters' positions more accurate in the client

This commit is contained in:
excitedneon 2017-05-09 19:42:28 +03:00
parent 105022427b
commit 7cde91340a
3 changed files with 28 additions and 3 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
using Cyber.Networking.Clientside;
namespace Cyber.Entities { namespace Cyber.Entities {
@ -25,6 +26,8 @@ namespace Cyber.Entities {
public Transform Head; public Transform Head;
private Vector3 MovementDirection = new Vector3(); private Vector3 MovementDirection = new Vector3();
private Vector3 ServerPosition = new Vector3();
private bool ServerPositionShouldLerpSync = false;
/// <summary> /// <summary>
/// Moves the character in the given direction. /// Moves the character in the given direction.
@ -90,15 +93,24 @@ namespace Cyber.Entities {
/// </summary> /// </summary>
/// <param name="reader"></param> /// <param name="reader"></param>
public override void Deserialize(NetworkReader reader) { public override void Deserialize(NetworkReader reader) {
Vector3 ServerPosition = reader.ReadVector3(); ServerPosition = reader.ReadVector3();
Vector3 ServerMovementDirection = reader.ReadVector3(); Vector3 ServerMovementDirection = reader.ReadVector3();
Vector3 ServerRotation = reader.ReadVector3(); Vector3 ServerRotation = reader.ReadVector3();
float Drift = (ServerPosition - transform.position).magnitude; 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; transform.position = ServerPosition;
MovementDirection = ServerMovementDirection; 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;
}
} }
/// <summary> /// <summary>
@ -111,6 +123,12 @@ namespace Cyber.Entities {
writer.Write(GetRotation()); writer.Write(GetRotation());
} }
private void Update() {
if (ServerPositionShouldLerpSync) {
transform.position = Vector3.Lerp(transform.position, ServerPosition, 10f * Time.deltaTime);
}
}
private void FixedUpdate() { private void FixedUpdate() {
CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime); CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime);
} }

View File

@ -6,7 +6,7 @@ namespace Cyber.Networking.Clientside {
/// Represents a connected player on the clientside. This class is used by clients. /// Represents a connected player on the clientside. This class is used by clients.
/// The C stands for "Client". /// The C stands for "Client".
/// </summary> /// </summary>
class CConnectedPlayer { public class CConnectedPlayer {
/// <summary> /// <summary>
/// Connection ID on the global perspective. /// Connection ID on the global perspective.

View File

@ -91,6 +91,13 @@ namespace Cyber.Networking.Clientside {
return Singleton.NetClient.isConnected; return Singleton.NetClient.isConnected;
} }
/// <summary>
/// Returns the connected player.
/// </summary>
/// <returns>The connected player.</returns>
public static CConnectedPlayer GetConnectedPlayer() {
return Singleton.Player;
}
private void Start() { private void Start() {
Spawner = GetComponent<Spawner>(); Spawner = GetComponent<Spawner>();