How to increase/decrease speed depending on the transform distance from target ?

I’m trying to make a follow catch up script logic.

When the transform distance is more then 5 from the target make the transform speed increasing to catch up the target when getting close to the target slow down and keep following the target.

If when running the game or in some cases the distance is more then 5 and the target is not moving increase the speed and slow down to stop near the target. but if the target is in a move then increase the speed until getting close to the target then slow down and keep follow the target.

When following the target increase/decrease the speed according to the target speed movement.

Now when running the game both conditions are true so it’s increasing the speed also when the transform already catch up the target even if the target is not moving at all so the transform hit the target in high speed instead slow down stop near the target.

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

public class Follow : MonoBehaviour
{
    public Transform targetToFollow;
    public Text text;
    public float lookAtRotationSpeed;
    public float moveSpeed;

    private float minMoveSpeed = 0f;

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

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 lTargetDir = targetToFollow.position - transform.position;
        lTargetDir.y = 0.0f;
        transform.rotation = Quaternion.RotateTowards(transform.rotation,
            Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);

        var distance = Vector3.Distance(transform.position, targetToFollow.position);

        text.text = distance.ToString();

        if(distance > 5f)
        {         
            moveSpeed += 0.5f * Time.deltaTime;
        }
       
        if (distance > 1.5f && distance < 5f)
        {
            moveSpeed += 0.7f;
        }
        else if (distance < 1f)
        {
            moveSpeed = Mathf.Max(minMoveSpeed, moveSpeed - 0.3f);
        }

        transform.position = Vector3.MoveTowards(transform.position, targetToFollow.position, Time.deltaTime * moveSpeed);
    }
}

There might be later more or other rules that can be add but for now I want to make some kind of catch up follow logic script. Something like when in game there is a dog that follow the player and walk “beside” the player.

There are multiply ways to do this, and the best way would probably be using a PID controller.

However, for starters, you can probably get by by simply lerping the follower’s speed between 0 and full speed using the distance divided by a factor (the maximum distance for example) as the lerping factor.

I think the problem is because you are adding/subtracting into your moveSpeed every frame, it gets kind of unpredictable.

Instead try to set it like:

if(distance > X && distance < Y)
    moveSpeed = normalSpeed * Z;

This way the moveSpeed will be set instead of summed or subtracted.

Also you could try to add a AnimationCurve in your class, so you it would be easier to edit your values and giving you smoother values, simply as that:

// class attribute
public AnimationCurve curve;
// FixedUpdate
moveSpeed = curve.Evaluate(distance);