How to achieve an undertale style camera

I’m looking for 2D camera, that will allow the player to move a certain radius before following him, like in undertale. i have written some code which does this but it jitters a bit and doesnt seem like a good way of going about it.

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

public class CameraFollowPlayer : MonoBehaviour
{
	public GameOb`enter code here`ject followTarget;
	private float cameraZ = 0;

	private Camera camera;

	public float cameraDistanceThreshold = 4f;

	public float speed = 4;

	

	void Start()
	{
		cameraZ = transform.position.z;
		camera = GetComponent<Camera>();
	}

	void FixedUpdate()
	{
		CheckForThreshold();
	}

	private void CheckForThreshold()
	{
		if (Vector2.Distance(followTarget.transform.position, transform.position) > cameraDistanceThreshold)
		{
			Vector3 delta = followTarget.transform.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, cameraZ));

			Vector3 destination = transform.position + delta;

			destination.z = cameraZ;

			transform.position = Vector3.MoveTowards(transform.position, destination, speed*Time.deltaTime);
		}
	}

}

I have looked everywhere. and for the love of god i cant find any clean solution. i will be extremely appreciative for any help i can get :slight_smile:

I’ve never played Undertale, but give the following script a shot:

using UnityEngine;

[RequireComponent(typeof(Camera))]
public class CameraFollowPlayer : MonoBehaviour
{
    public Transform Target;
    public float MaxDistance = 4f;
    public float Speed = 4;

    private new Camera camera;
    private Vector3 viewportPoint;
    private Vector3 desiredPosition;

    void Start()
    {
        camera = GetComponent<Camera>();
        viewportPoint = new Vector3( 0.5f, 0.5f, Mathf.Abs( transform.position.z - Target.position.z ) );
        desiredPosition = transform.position;
    }

    void LateUpdate()
    {
        FollowTarget();
    }

    private void FollowTarget()
    {
        Vector3 centerPoint = camera.ViewportToWorldPoint( viewportPoint );
        Vector3 deltaPosition = Target.position - centerPoint;

        if ( deltaPosition.sqrMagnitude > MaxDistance * MaxDistance )
            desiredPosition = transform.position + deltaPosition;

        transform.position = Vector3.MoveTowards( transform.position, desiredPosition, Speed * Time.deltaTime );
    }

}