Making an AI for an NPC

I’m making a 2D RPG style game and was running into issues with making an NPC “companion” fallow behind the player. the idea i had was that the NPC would fallow a way point (that being the player) and animate itself as it fallows. The problem I have is that after the player animations stop, the companions animations stop. I need help making it so that the animations continues until it reaches the player. I used similar controls for the companion that i did for the player. Help would be much appreciated.

My Code:

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

public class WayPointFallow : MonoBehaviour
{

private Animator anim;

private bool companionMoving;
private Vector2 lastMove;

public GameObject wayPoint;
//This is how often your waypoint's position will update to the player's position
private float timer = 0.5f;

public GameObject target;
public List<Vector3> positions;
public int distance_permitted;
public float speed;
private Vector3 lastLeaderPosition;

void Start()
{
    anim = GetComponent<Animator>();

    positions.Add(target.transform.position);
}

void Update()
{

    companionMoving = false;

    if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
    {

        companionMoving = true;
        lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    }

    if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
    {

        companionMoving = true;
        lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    }
    anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
    anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
    anim.SetBool("CompanionMoving", companionMoving);
    anim.SetFloat("LastMoveX", lastMove.x);
    anim.SetFloat("LastMoveY", lastMove.y);

    if (timer > 0)
    {
        timer -= Time.deltaTime;
    }
    if (timer <= 0)
    {
        //The position of the waypoint will update to the player's position
        UpdatePosition();
        timer = 0.5f;
    }

    if (lastLeaderPosition != positions[positions.Count - 1])
    {
        positions.Add(target.transform.position);
    }

    if (positions.Count >= distance_permitted)
    {
        if (gameObject.transform.position != positions[0])
        {

            transform.position = Vector3.MoveTowards(transform.position, positions[0], Time.deltaTime * speed);

        }
        else
        {
            positions.Remove(positions[0]);
            gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
            transform.position = Vector3.MoveTowards(transform.position, positions[0], Time.deltaTime * speed);
        }
    }
    lastLeaderPosition = target.transform.position;

}

void UpdatePosition()
{
    //The wayPoint's position will now be the player's current position.
    wayPoint.transform.position = transform.position;
}

}

Here, your “companionMoving” is only true when you are geting axis data (so, when you are moving the joystick)

you should probably set it to false when he got to his destination, then setting it to tru again once his target has moved.