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.")]
public float positionSensitivity = 0.01f;
[Header("Smoothing")]
public bool smoothCorrection = true;
void Awake()
{
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()
{
if (Vector3.Distance(transform.position, lastPosition) >= positionSensitivity)
@ -51,10 +72,7 @@ public override void OnDeserialize(NetworkReader reader, bool initialState)
Vector3 velocity = reader.ReadVector3();
// hard force for now.
// TODO compare past position at timestamp, and only correct if needed
rb.position = position;
rb.rotation = rotation;
rb.velocity = velocity;
ApplyState(position, rotation, velocity);
}
}
}