Pavel Nakonechnyy
Digital Leadership & Soft Skills

Unity | Smooth camera follow 2D

Published in GameDev.

Unity | Smooth camera follow 2D

Camera with this small script will follow Target with gap depending on its speed.

using System.Collections;

using UnityEngine;

public class SmoothFollow2D : MonoBehaviour {
    public Transform Target;
    public Vector3 Offset;
    public float Velocity;
    public float MinDistance;

    // Update is called once per frame
    void LateUpdate() {
        if (Target == null) {

        var targetPos = Target.transform.position + Offset;

        if (Vector3.Distance(transform.position, targetPos) < MinDistance) {
        var newPos = Vector3.Lerp(transform.position, targetPos, Velocity * Time.fixedDeltaTime);