unity 2d smooth camera follow code example

Example 1: smooth camera follow 2d

using UnityEngine;

public class CameraFollow2D : MonoBehaviour
{
    public float FollowSpeed = 2f;
    public Transform Target;

    private void Update()
    {
        Vector3 newPosition = Target.position;
        newPosition.z = -10;
        transform.position = Vector3.Slerp(transform.position, newPosition, FollowSpeed * Time.deltaTime);
    }
}

Example 2: how to make camera follow player unity 2d

public class CameraFollow : MonoBehaviour {

    public GameObject Target;
    private Vector3 Offset;


    // Start is called before the first frame update
    void Start() {

        Offset = transform.position - Target.transform.position;
        
    }

    // Update is called once per frame
    void Update() {
    
        transform.position = Target.transform.position+Offset;

        
    }
}

Example 3: camera follow player unity smooth

// CAMERA FOLLOW PLAYER - IN FIXEDUPDATE

Vector3.SmoothDamp(cam.position, transform.position + cameraOffset, ref cameraVelocity, CameraFollowSmoothTime);
cam.position = cam.position + cameraVelocity * Time.deltaTime;