Smooth Camera Zoom on movement not working

I’m trying to get my camera to zoom depending on how fast the player is moving. So the faster you go, the further out the camera zooms (orthagonal size increases) so you can see what’s ahead. On acceleration and normal deceleration it works fine, but when you crash into something (And velocity gets to near zero instantly) the camera jerks down to minimum distance in one frame, which isn’t smooth at all. Not sure where I should go from here, here’s the code I’m using at the moment:

public class CameraMovement : MonoBehaviour {

public Transform ship;
public Camera mainCamera;

public float xBound = 6.0f;
public float yBound = 3.0f;
public float camHeight = 10.0f;
public float camMaxHeight = 10.0f;
public float dampMove = 2.0f;
public float speedAdjuster = 1.0f;

private Rigidbody2D shipRB;

private float xPos;
private float yPos;
private float zPos;

private float oldPosition = 0;
private float newPosition = 0;

void Start ()
{
	shipRB = ship.GetComponent<Rigidbody2D>();
}

void Update ()
{
	xPos = ship.position.x + Mathf.Clamp(shipRB.velocity.x / dampMove, -xBound, xBound);
	yPos = ship.position.y + Mathf.Clamp(shipRB.velocity.y / dampMove, -yBound, yBound);
	zPos = -10;

	oldPosition = newPosition;
	newPosition = Mathf.Clamp(shipRB.velocity.magnitude / dampMove, 0.0f, (camMaxHeight - camHeight)); 

	mainCamera.orthographicSize = camHeight + Mathf.Lerp(oldPosition, newPosition, Time.deltaTime*speedAdjuster);

	transform.position = new Vector3 (xPos, yPos, zPos);

}

}

What do I need to do/change to enable a smoother camera zoom (orthagonal size movement)?

You are lerping the orthographic size, but you arent lerping the cameras position.

transform.position = Vector3.Lerp(transform.position, new Vector3(xPos, yPos, zPos), Time.deltaTime * speedAdjuster);

@Alan Wyvern

Could you set a conditional up that has a default setting anytime velocity is less than a certain value? like say you have the same view distance for 0-2 velocity units, and then you use your algorithm for the rest of the speeds?

If that still causes too much that isn’t smooth you could have crashes stop your normal controls and run a co-routine that takes you from the current speed to the zero speed and back to the players speed until back to normal. if i am writing that in a way that makes sense. Could have a Steady() and a Crash() and have the Steady() Stopped when a crash is detected and then start the Crash() and then vice versa to come out of it.

Those are the two ideas I came up with. Let me know if you continue to find difficulties and I will take a bit more time with it and see what I can do.

Hope this helps, let me know!

Got to the bottom of this one, I was updating the from position on the lerp when really I shouldn’t have done that. Here’s the working code:

void Update()
{
	Vector3 offsetTarget = new Vector3(Mathf.Clamp(shipRB.velocity.x / dampMove, -xBound, xBound),
                                       Mathf.Clamp(shipRB.velocity.y / dampMove, -yBound, yBound),
									   0.0f);
	
	float distanceTarget = Mathf.Clamp(shipRB.velocity.magnitude / dampMove, 0.0f, (camMaxHeight - camHeight));

	offset = Vector3.Lerp(offset, offsetTarget, Time.deltaTime * speedAdjuster);
	distance = Mathf.Lerp(distance, distanceTarget, Time.deltaTime * speedAdjuster);

	mainCamera.orthographicSize = camHeight + distance;

	transform.position = new Vector3(ship.position.x, ship.position.y, -10) + offset;
}