简体   繁体   中英

How can i make a smooth camera transition in Unity 2D?

I have an endless Paralax effect background which i want to move when clicking a button. So basically i want to move my camera about 20 units to the left or right every time i click a button, i kinda achieved this already, but i want the transition to be smooth, now it is all happening between two frames, thanks for any help, I'm desperate.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMover : MonoBehaviour
{

    public float moveAmount = 19.2f;

    public void ButtonLeft ()
    {
        Camera.main.transform.Translate(-moveAmount,0,0);
    }

    public void ButtonRight ()
    {
        Camera.main.transform.Translate(moveAmount,0,0);
    }
    

}

Smooth transitions can be done with the Vector3.Lerp function. You can find detailed documentation and usage examples in the official Unity documentation .

Your process should be that you specify a target and a start position of the camera. Then you lerp the position of the camera on each Update() until you reach the target.

A similar answer can also be found on the unity forums .

Have fun: :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM