GameObject Moving Too Fast For Camera Tracking (2D)

I have a character GameObject that you can AddForce to. There is also a script on the camera to follow that character GameObject. When AddForce gets called on the GameObject too much, it starts moving too fast for the camera to keep up. Here is the script for following the character (in Update()):

    Vector3 from = transform.position;
    from.x = originalX;
    Vector3 to = target.position;
    to.z = transform.position.z;
    to.x = originalX;

    transform.position -= (from - to) * Time.deltaTime;

OriginalX is a variable that just grabs the original Camera x coordinate, as I don’t want the Camera to follow the character in the x coordinate. So is there something I can do to make the Camera keep up with a fast moving object? Or something I can do to the Player script?

Thanks!

Your delta movement:

transform.position -= (from - to) * Time.deltaTime;

Is using the distance between the object and the camera as a speed. That speed will move it towards the target but isn’t guaranteed to keep up with it - like a elastic band.

You will need to check for the distance becoming too great, and force the camera to keep up. Try this:

Vector3 delta = (to - from);
float linearDist = delta.Magnitude();
if (linearDist > maxSeperation)
{
    delta.Normalise();
    delta *= (linearDist - maxSeperation);
    transform.position += delta;   // no timeDelta in this case
}
else
{
    transform.position += delta * Time.deltaTime;
}

(Where maxSeperation is how far you’re willing to let the camera lag behind)

That will make the camera follow as if attached by a chain as well as the elastic. For small movements the elastic will pull it, but when that chain gets taut the camera won’t lag behind. You can also make the “elastic” tighter by multiplying the delta by (Time.timeDelta * elasticStrength) instead…

You might also want to add a bit of target lookahead proportional to velocity so you can see more of where you’re going.