0

I have a object that moves at a variable speed. It can reach from 0 speed units up to N speed units.

I'm lerping the camera angle to rotate and look at the object smoothly and nice, like this:

camera.eulerAngles = Vector3.Lerp(initialAngle, finalAngle, speed / N);

The problem is when the object collides and the velocity decrease instantaneously to 0, the lerp happens very abruptly.

How can I make it handle this situation? I'd like a fast interpolation in this case, but no instantaneous like I'm seeing.

Daniel
  • 7,357
  • 7
  • 32
  • 84

1 Answers1

2

You can clamp the speed delta per iteration:

private const float MAX_DELTA = 0.1F;
private float prevSpeed;

private void UpdateRotation() {
    float currentSpeed = Mathf.Clamp(speed, prevSpeed - MAX_DELTA, prevSpeed + MAX_DELTA);
    camera.eulerAngles = Vector3.Lerp(initialAngle, finalAngle, currentSpeed / N);
    prevSpeed = currentSpeed;
}
Thomas
  • 1,225
  • 7
  • 16