Smooth camera movement between two targets.

Rather than, without a transition, switching from one camera to another one, I want the camera to move from the player to the target. Since I want to make it move smoothly (slow, than fast, then slowing down again), I assume this uses "lerp". Unfortunately I'm a complete newbie at this stuff. Help, please.

Also, the camera is currently in a first-person perspective with the player. It will be togglable to third-person later, but I could probably alter the script to my liking around then.

You'll have to use one camera only, that lerps between one point to another. You didn't give any code as to what first person view script you are using so I'm not sure how your code works, but generally you can store the Transform of the target, then make your camera lerp to the position of that target.

Here is some generic code to make a Vector3 lerp transition (C#). You'll want to put this in a script for your camera:

public float transitionDuration = 2.5f;
public Transform target;
IEnumerator Transition()
{
    float t = 0.0f;
    Vector3 startingPos = transform.position;
    while (t < 1.0f)
    {
        t += Time.deltaTime * (Time.timeScale/transitionDuration);

        transform.position = Vector3.Lerp(startingPos, target.position, t);
        yield return 0;
    }
}

Assign a value to target by creating an empty GameObject, putting it at the place you want, then drag it to the target variable. You can initiate a transition by using:

StartCoroutine(Transition());

anywhere in the same file where the transition code is in.

For anyone else that wants a more specialized lerp function, i added my rendition, totally based off of the great work by @anomalous_underdog. Thanks dude! This is used to move the camera to a specific point and the relative speed thing are the two bounds in which the camera can move between.

That way you can use it like this (where camPanDuration is either a variable or you can put in numbers on the fly, farLeft.position is where i want to lerp to and true just uses relative time). It’s pretty specific to moving something between two points.

 public void resetCam()
    {
      StartCoroutine(LerpToPosition(camPanDuration, farLeft.position, true));    
    }

    IEnumerator LerpToPosition(float lerpSpeed, Vector3 newPosition, bool useRelativeSpeed = false)
    {    
        if (useRelativeSpeed)
        {
            float totalDistance = farRight.position.x - farLeft.position.x;
            float diff = transform.position.x - farLeft.position.x;
            float multiplier = diff / totalDistance;
            lerpSpeed *= multiplier;
        }

        float t = 0.0f;
        Vector3 startingPos = transform.position;
        while (t < 1.0f)
        {
            t += Time.deltaTime * (Time.timeScale / lerpSpeed);

            transform.position = Vector3.Lerp(startingPos, newPosition, t);
            yield return 0;
        }    
    }

Hope that helps someone!

I am implementing a similar behaviour at which I have added a reorientation of the camera towards the target. It works, however when the target object is behind the camera, the camera goes down in a weird move. I would like to prevent it from going too much towards the ground during the travelling (that is to say that the angle between the camera’s forward and the ground should never be greater than 80°).

Does anyone have an idea on how to implement such a behaviour ?

Thanks in advance,