Fix some more of the movement jitter problems

This commit is contained in:
excitedneon 2017-05-09 18:59:12 +03:00
parent 1df766f662
commit 419ac77a6a

View File

@ -25,8 +25,6 @@ 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 SyncToServerPosition = false;
/// <summary> /// <summary>
/// Moves the character in the given direction. /// Moves the character in the given direction.
@ -92,12 +90,11 @@ 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) {
ServerPosition = reader.ReadVector3(); Vector3 ServerPosition = reader.ReadVector3();
float Drift = (ServerPosition - transform.position).magnitude; float Drift = (ServerPosition - transform.position).magnitude;
if (Drift > 1) { if (Drift > MovementSpeed * 0.5f) {
SyncToServerPosition = true; transform.position = ServerPosition;
} else if (Drift < 0.01) { MovementDirection = reader.ReadVector3();
SyncToServerPosition = false;
} }
Vector3 rot = reader.ReadVector3(); Vector3 rot = reader.ReadVector3();
} }
@ -108,19 +105,10 @@ namespace Cyber.Entities {
/// <param name="writer"></param> /// <param name="writer"></param>
public override void Serialize(NetworkWriter writer) { public override void Serialize(NetworkWriter writer) {
writer.Write(transform.position); writer.Write(transform.position);
writer.Write(MovementDirection);
writer.Write(GetRotation()); writer.Write(GetRotation());
} }
private void Update() {
if (SyncToServerPosition) {
// LERP version
//transform.position = Vector3.Lerp(transform.position, ServerPosition, 50f * Time.deltaTime);
// Non-LERP version
transform.position = ServerPosition;
SyncToServerPosition = false;
}
}
private void FixedUpdate() { private void FixedUpdate() {
CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime); CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime);
} }