Hello,
There are many posts about this, but I haven’t found a suiting answer yet.
I am making a side scrolling racing game like “Hill Climb Racing” or “MMX Hill Dash”. The camera script is using the standard Lerp method on FixedUpdate.
The problem is that the camera will never reach the endPosition until the vehicle stops. And as the vehicle goes faster, the camera will lag behind even further. What I want is a natural camera behavior as in real life. When the vehicle starts, the camera must lag behind, as if the cameraman is not fast enough to follow the starting movement of the car. But once the car is moving the cameraman has the car perfectly centered in view. And when the driver hits the brakes, the camera will overshoot at first, and then correct again.
So somehow, I have figure out a way to set damping to zero, but I can’t figure out a good solution because the speed and position of the vehicle is constantly changing.
I am thinking to compare the velocity of the car and camera might be another solution, but how…?
Cinemachine is a great camera package available in the PackageManager. You can do all sorts of fancy camera effects. If you know how to use it, it can be quite the powerful tool
Thx for the tip. But at first sight the problem stays. Damping actually is Lagging.
They made a nice feature called LookAhead which could hide my problem if I would make that dynamic. But that still requires a logic solution with some kind of tweening. So I don’t need a 6Mb pack when I just need five lines of code. Thx anyways, it is a nice package for a side scrolling game, but not the solution for a race game.
Maybe try something like a spring camera. If you attach a spring to your car with the camera on the end of the spring then it would lag at first as the car accelerates. When the car reaches a constant velocity the spring would move back to follow the car exactly. When the car brakes the spring would overshoot too.
Yes that is the idea, but the code you are providing doesn’t seam to do that dynamically. When you pressed fire2 it will use ApplySnapping(), and by default it will ApplyPositionDamping() which uses SmoothDamp (which has the same problem as Lerp). So there is still no dynamic logic, it happens on a press of a button.
// Set Camera X
float newCamPos = CamX + velocity + catchUpVelocity;
transform.position = new Vector3( newCamPos, Pos.y, Pos.z );
// Set prev to get Velocity on next frame
prevTargetX = TargetX;
}
Explanation:
The velocity provides a momentum to make the elastic effect work, and the catchUpVelocity moves the camera back to target.
Without the catchUpVelocity (catchUpDamping = 0f). At start, both camera and car are standing still, the car starts moving and slowly the camera starts moving to. After a few seconds the camera and vehicle have equal speeds, but the camera is far behind. So now we have to add a little bit of extra speed to catch up with the car.
Work needed:
While accelerating, the camera is still lagging, but not as much as the first version. This is fine for this game, but may change in the future. What is better in this version is, when top speed is reached the camera will center to the car, and this never happens in the first version, in the first version the camera would lag behind too far at high speeds.