# A way to make camera get distance from the player when the player gains velocity?

I'm building upon roll a ball example and I'm trying to make the camera gain distance from the player, when the player starts moving (gaining velocity). i came up with this, but it feels clunky and the camera jumps. is there a better way to achieve this?

public float MiddleLineForCameraAdjuster = 2.4f;
float velocityCounter, offsetCounter, normalizedVelX, normalizedVelY, normalizedVelZ;

{
if (rb.velocity.x < 0) normalizedVelX = -rb.velocity.x;
else normalizedVelX = rb.velocity.x;
if (rb.velocity.y < 0) normalizedVelY = -rb.velocity.y;
else normalizedVelY = rb.velocity.y;
if (rb.velocity.z < 0) normalizedVelZ = -rb.velocity.z;
else normalizedVelZ = rb.velocity.z;

if (normalizedVelX + normalizedVelY + normalizedVelZ < 25)
{
velocityCounter = normalizedVelX + normalizedVelY + normalizedVelZ;
}
else if (normalizedVelX + normalizedVelY + normalizedVelZ >= 25)
{
velocityCounter = 25;
}
else velocityCounter = 0;

if (normalizedVelX > 0 || normalizedVelY > 0 || normalizedVelZ > 0)
{
offsetCounter = 2.4f + (velocityCounter / 10);
CameraController.offset.x = (CameraController.offset.x * offsetCounter) / MiddleLineForCameraAdjuster;
CameraController.offset.z = (CameraController.offset.z * offsetCounter) / MiddleLineForCameraAdjuster;
}
}

offset = (transform.position - Player.transform.position);

• Without beeing able to test your code right now I would not tie your camera position directly to the velocity but instead add some sort of smoothing/easing inbetween to make it more natural. Aug 10, 2016 at 6:59
• I actually don't like it this way either, but I'm new to coding in general and this was the best i could come up with. Can you suggest a way to add smoothing? Also a way to prevent the camera to jump when there's a dramatic change in velocity? Or an alternative to my solution? Thanks Aug 10, 2016 at 7:25
• I'm not exactly sure. I did something like this long time ago. I'd propably take a look at docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html Hope that helps. Also you could just google 'unity smooth camera' or something similar. There's lots of stuff on that around. Aug 10, 2016 at 7:43

If you want the camera to simply increase the distance relative to the velocity I would do something like:

float cameraDistanceMultiplier = DefaultCamDistance * velocity.length;


Camera.Position = Camera.Position + (newCamPos - Camera.Position) * 0.9f; //tune this value