Camera Follow Script Unity


Here is an example of a script that you can use to make a camera follow a target in Unity:

using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform target;
    public float smoothTime = 0.3f;

    private Vector3 velocity = Vector3.zero;

    void LateUpdate()
    {
        Vector3 targetPosition = target.TransformPoint(new Vector3(0, 5, -10));
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
    }
}



To use this script, attach it to the camera GameObject and set the target Transform in the Inspector. The camera will smoothly follow the target with the specified offset (in this case, 5 units above and 10 units behind the target). You can adjust the smoothTime variable to control how smoothly the camera moves.