Enemy not Moving Towards player when animator inside enemy

So im trying to make the enemy to follow the player. But the problem is it doesnt follow if theres an animator in the enemy object.

Enemy script:

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

public class Enemy : MonoBehaviour
{
    public Animator animator;
    public int maxHealth = 100;
    public float speed;

    private Transform target;

    int currentHealth;

    public GameObject ThisEnemy;

    public EnemyHealthBar healthBar;

    private void Start()
    {
        currentHealth = maxHealth;
        healthBar.Sethealth(currentHealth, maxHealth);
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    }

    private void Update()
    {
        transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    }

    public void TakeDamage(int damage)
    {
        currentHealth -= damage;
        healthBar.Sethealth(currentHealth, maxHealth);

        animator.SetTrigger("Hit");

        if(currentHealth <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        Debug.Log("Enemy is ded");

        animator.SetBool("IsDead", true);

        GetComponent<Collider2D>().enabled = false;
        this.enabled = false;
        Destroy(ThisEnemy);
    }
}

I have an animator that goes from Entry → IdleEnemy and AnyState → Hit(if condition hit is triggered) → Dead enemy(IsDead=true) or Idle enemy(IsDead=false)

Hi!

When you put movement and animations on the same object, the animator will usually override any positional data. Try putting the art and any animations onto a child gameobject which will handle visuals, and let the parent handle movement. This way, none of them will override each other.

Hope this helps :slight_smile: