I have camera a that is actively following an object (a plane) from behind. This object can accelerate or decelerate.

I would like to have a camera offset when I move the plane, so it doesn't look so static.

I have done this with the following code:

float cameraOffset = 0.05 * elapsed_time * 100;

Vector3 previousEye = camera->eye;  
Vector3 newEye = ship->model * Vector3(0, 10, -5);
Vector3 eye = previousEye * (1 - cameraOffset) + newEye * cameraOffset;

Vector3 previousCenter = camera->center;
Vector3 newCenter = ship->model * Vector3(0, 0, 10);
Vector3 center = previousCenter * (1 - cameraOffset) + newCenter * cameraOffset;

Vector3 up = ship->model.rotateVector(Vector3(0, 1, 0));

camera->lookAt(eye, center, up);
previousModel = ship->model;

It works fine, except that when I increase the object's speed, the camera barely follows it. This is of course logical; my code is supposed to make it difficult for the camera to follow the plane.

However, I would like to have a lateral camera offset and also to keep following the plane from a relatively short distance.

Any hint on how to achieve this is appreciated.

  • \$\begingroup\$ You could keep track of the last n positions of the plane, and make your camera offset from the oldest one. \$\endgroup\$
    – Peethor
    May 2 '19 at 5:15

I have something similar in a 2D game I'm making in Unity, but instead I am changing the transform using the Mathf.Lerp function. This interpolates between two numbers. I record my current position, and interpolate to the position I want the camera to be. Mathf.SmoothStep does the same thing but has some more smoothing added in.

I hope this helps. I am very new to this ;D

  • \$\begingroup\$ Peehaps you can elaborate your answer with a code example? Also what the concept of a math.lerp helps to answer the question? \$\endgroup\$
    – Felsir
    May 2 '19 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.