Help with Chase AI? (Similar to Blue Demon)

all! I'm working on a chase ai / follow player script, for a 2D project of mine, I'm going for something similar to Blue Demon where an integer value controls what enemy is on screen, and can be changed for individual instances, and it follows the players animations and directions closely, and when you make contact with the enemy, you are defeated- game over. Anyways, I've got some code 'somewhat' working, i have an enumeration that controls the state machine of the animator, however, The Update loop is the real problem here, as of right now-

// Update is called once per frame void Update () {

    //bool isMoving = anim.GetBool("isMoving");
if (EnemyID == 0)
{
//transform.position = Vector3.MoveTowards(self.position, player.position, speed * Time.deltaTime);

if(Vector3.Distance(target.position, transform.position) <= maxRange && Vector3.Distance(target.position, transform.position)>= minRange)
{
anim.SetBool("isMoving", true);
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
}

// Enter State
MovingState();
}
else {
IdleState();
}


}

Instead of the object following the player, it starts to run away when the player gets near? or when the player goes to a corner, it moves diagonally? It never seems to actually follow the player consistently. Suggestions and constructive criticism are greatly appreciated, thanks in advance!

Edit:

    Animator anim;
bool caught;
public Animator player;
public Transform target;
[SerializeField]
float speed = 5f;
[SerializeField]
float maxRange;
[SerializeField]
float minRange;

public int EnemyID;
State state;


The EnemyID as of now isn't necessarily set to a specific value (i.e, it is set to 0) When it is equal to that number, the Update loop is supposed to have the object follow the player at a specific speed and range, during this time, the anim reference is setting the animator boolean parameter 'isMoving' to true starting the walking animation. If the enemy object stops moving, it should enter its idle state.

    IEnumerator IdleState()
{
yield return new WaitForEndOfFrame();

anim.Play("Idle");
}

IEnumerator MovingState()
{
yield return new WaitForEndOfFrame();

anim.Play("Move");
}


Currently the idle state is not working, but this is alright.

• You might want to show us the code in MovingState too, and show us how your EnemyID, speed, and target variables get assigned. Aug 23, 2020 at 0:04
•  Animator anim; bool caught; public Animator player; public Transform target; [SerializeField] float speed = 5f; [SerializeField] float maxRange; [SerializeField] float minRange; public int EnemyID; State state;  Aug 23, 2020 at 0:13
• You can edit your question to include a more complete code sample. Important information like that shouldn't be just in a comment. Aug 23, 2020 at 0:13
• Ah, fair point, doing that rn. Aug 23, 2020 at 0:13
• Edited the post to include this information. Aug 23, 2020 at 0:25