What to do to move camera smoothly from one place to another in 2d?

Hello,

I don’t know how to say it technically. So let me explain.

I want the camera to change from one position to another without teleporting. For instance, Camera is at the X = 0 where the player is and then I want it to move to X=10 without instant teleporting. How do I achieve this.

In filming it’s call Dolly. Whatever it is, I just don’t want to use transform.position because it will teleport from x=0 to x=10.

Sorry, English is not my native language. Is you not understand about what I’m saying, please tell me. I want to have this cool feature in my game.

You can use Lerp or MoveTowards. I would suggest Lerp.

Lerp (linear interpolation) lets you input a start and end position and then provide a percentage between.

float t = 0.0f;

void Update()
{
    transform.position = Vector3.Lerp(start, end, t);
    t += Time.deltaTime;
}

Something like that. I threw that together without testing and while tired, so it may need tweaking.

This case will move between start and end in a second, because when t is 0 then it returns the start and when t is 1, it returns the end. You can multiply or divide the t’s increment to control the speed.

The position of the camera is the position of the current player. For instance,

float posX = player.position.x;
        posX += 5;
transform.position = new Vector3 ( posX, transform.position.y, transform.position.z);

As you can see the position of the camera is “player.position.x” + 5. And what I want to do is change the 5 to 10 and make it move smoothly but if I use transform.position, the camera will jump to that position of 10 instantly.

If you still don’t understand what I’m saying then what I want to do is an adjustable overhead camera so player can choose to change the position of the camera in real time by pressing “>” on keyboard.

Lerp will do what you want. There’s no such thing as smoothly moving between two positions in games. Instead, you ‘teleport’ by tiny amounts each frame and it gives the illusion of smooth movement.

In my example, each time Lerp is called in update, it returns a slightly higher percentage of the distance and so the position is adjusted by a very small amount.

Thank you mate. I found out another method called “Mathf.SmoothDamp” It shorter than using Lerp.