fix(Prediction): PredictedRigidbodyVisual rotation interpolation now works

This commit is contained in:
mischa 2023-12-19 14:11:09 +01:00
parent 3d87b3f0c3
commit 58148e02e5
2 changed files with 13 additions and 7 deletions

View File

@ -93,7 +93,8 @@ public class PredictedRigidbody : NetworkBehaviour
public Material ghostMaterial;
[Tooltip("How fast to interpolate to the target position, relative to how far we are away from it.\nHigher value will be more jitter but sharper moves, lower value will be less jitter but a little too smooth / rounded moves.")]
public float interpolationSpeed = 15; // 10 is a little too low for billiards at least
public float positionInterpolationSpeed = 15; // 10 is a little too low for billiards at least
public float rotationInterpolationSpeed = 10;
[Tooltip("Teleport if we are further than 'multiplier x collider size' behind.")]
public float teleportDistanceMultiplier = 10;
@ -124,7 +125,8 @@ protected virtual void CreateVisualCopy()
// add the PredictedRigidbodyVisual component
PredictedRigidbodyVisual visualRigidbody = visualCopy.AddComponent<PredictedRigidbodyVisual>();
visualRigidbody.target = this;
visualRigidbody.interpolationSpeed = interpolationSpeed;
visualRigidbody.positionInterpolationSpeed = positionInterpolationSpeed;
visualRigidbody.rotationInterpolationSpeed = rotationInterpolationSpeed;
visualRigidbody.teleportDistanceMultiplier = teleportDistanceMultiplier;
// copy the rendering components

View File

@ -11,7 +11,8 @@ public class PredictedRigidbodyVisual : MonoBehaviour
Rigidbody targetRigidbody;
// settings are applied in the other PredictedRigidbody component and then copied here.
[HideInInspector] public float interpolationSpeed = 15; // 10 is a little too low for billiards at least
[HideInInspector] public float positionInterpolationSpeed = 15; // 10 is a little too low for billiards at least
[HideInInspector] public float rotationInterpolationSpeed = 10;
[HideInInspector] public float teleportDistanceMultiplier = 10;
// we add this component manually from PredictedRigidbody.
@ -49,13 +50,16 @@ void LateUpdate()
// smoothly interpolate to the target position.
// speed relative to how far away we are
float step = distance * interpolationSpeed;
float positionStep = distance * positionInterpolationSpeed;
// speed relative to how far away we are.
// => speed increases by distance² because the further away, the
// sooner we need to catch the fuck up
// float step = (distance * distance) * interpolationSpeed;
transform.position = Vector3.MoveTowards(transform.position, targetRigidbody.position, step * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRigidbody.rotation, step * Time.deltaTime);
// float positionStep = (distance * distance) * interpolationSpeed;
transform.position = Vector3.MoveTowards(transform.position, targetRigidbody.position, positionStep * Time.deltaTime);
// smoothly interpolate to the target rotation.
// Quaternion.RotateTowards doesn't seem to work at all, so let's use SLerp.
transform.rotation = Quaternion.Slerp(transform.rotation, targetRigidbody.rotation, rotationInterpolationSpeed * Time.deltaTime);
}
}
}