MovePosition/Rotation

This commit is contained in:
mischa 2023-09-06 19:33:50 +02:00
parent 0c1cab6ea2
commit 0b00351822

View File

@ -13,11 +13,32 @@ public class PredictedRigidbody : NetworkBehaviour
[Tooltip("Broadcast changes if position changed by more than ... meters.")] [Tooltip("Broadcast changes if position changed by more than ... meters.")]
public float positionSensitivity = 0.01f; public float positionSensitivity = 0.01f;
[Header("Smoothing")]
public bool smoothCorrection = true;
void Awake() void Awake()
{ {
rb = GetComponent<Rigidbody>(); rb = GetComponent<Rigidbody>();
} }
void ApplyState(Vector3 position, Quaternion rotation, Vector3 velocity)
{
// Rigidbody .position teleports, while .MovePosition interpolates
// TODO is this a good idea? what about next capture while it's interpolating?
if (smoothCorrection)
{
rb.MovePosition(position);
rb.MoveRotation(rotation);
}
else
{
rb.position = position;
rb.rotation = rotation;
}
rb.velocity = velocity;
}
void UpdateServer() void UpdateServer()
{ {
if (Vector3.Distance(transform.position, lastPosition) >= positionSensitivity) if (Vector3.Distance(transform.position, lastPosition) >= positionSensitivity)
@ -51,10 +72,7 @@ public override void OnDeserialize(NetworkReader reader, bool initialState)
Vector3 velocity = reader.ReadVector3(); Vector3 velocity = reader.ReadVector3();
// hard force for now. // hard force for now.
// TODO compare past position at timestamp, and only correct if needed ApplyState(position, rotation, velocity);
rb.position = position;
rb.rotation = rotation;
rb.velocity = velocity;
} }
} }
} }