0
\$\begingroup\$

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;

//Camera Adjuster
private void CameraAdjuster()
{
    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;
            MiddleLineForCameraAdjuster = offsetCounter;
    }
}

offset = (transform.position - Player.transform.position);
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Aug 10, 2016 at 6:59
  • \$\begingroup\$ 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 \$\endgroup\$
    – MrChips
    Aug 10, 2016 at 7:25
  • \$\begingroup\$ 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. \$\endgroup\$ Aug 10, 2016 at 7:43

1 Answer 1

2
\$\begingroup\$

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

float cameraDistanceMultiplier = DefaultCamDistance * velocity.length; 

if(cameraDistanceMultiplier < MIN_CAMERA_DISTANCE)
    cameraDistanceMultiplier = MIN_CAMERA_DISTANCE;

if(cameraDistanceMultiplier > MAX_CAMERA_DISTANCE)
    cameraDistanceMultiplier = MAX_CAMERA_DISTANCE;

newCamPos = cameraDistanceMultiplier * CameraDirection;

To add a rubbery feel to it you could do:

Camera.Position = Camera.Position + (newCamPos - Camera.Position) * 0.9f; //tune this value
\$\endgroup\$
1
  • \$\begingroup\$ this couldn't help in my situation, but its a good code. thanks anyways. \$\endgroup\$
    – MrChips
    Aug 21, 2016 at 5:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .