animation get stuck on enemy's

i Instantiate enemy’s and there animations work just fine first. there idle and when they find a waypoint they walk to it. after a few seconds the animations wont stop. the enemy is standing still but the walking animation is played. or they are moving and the idle animation is played.

however when i have just 1 enemy everything works just fine. how is that?!
if i need to post more information please tell me then!

the code is in the spoiler:

public Transform player;
    public int range = 8;
    public int fieldOfView = 40;
    public int minRange = -2;
    public int maxRange = 2;
    public float enemySpeed = 2f;
    public GameObject WaypointPrefab;
    public bool wayP = false;
    public float countDown = 5;

    private float timer;
    private bool foundPlayer = false;
    static Animator anim;
    GameObject WP;

    private void Start()
    {
        anim = GetComponent<Animator>();
        timer = countDown;
    }

    void Update()
    {
        if (!wayP)
        {
            timer -= Time.deltaTime;
        }
        if (timer < 0 && !foundPlayer && !wayP)
        {
            createWayPoint();
            timer = countDown;
        }
        if(wayP == true){
            move();
        }
        if(!foundPlayer && !wayP)
        {
            animIdle();
        }
        
        Vector3 direction = player.position - this.transform.position;
        float angle = Vector3.Angle(direction, this.transform.forward);
       
        if (Vector3.Distance(player.position, this.transform.position) < range && angle < fieldOfView)
        {
            direction.y = 0;
            foundPlayer = true;

            this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);

            anim.SetBool("isIdle", false);
            if (direction.magnitude > 3)
            {
                Vector3 walkTarget = player.transform.position;
                walkTarget.y = 0.0f;
                this.transform.position = Vector3.MoveTowards(this.transform.position, walkTarget, enemySpeed * Time.deltaTime);
                animWalk();
            }else
            {
                animAttack();
            }
        }else
        {
            foundPlayer = false;
        }
    }

    void createWayPoint()
    {
        if (!wayP)
        {
            float distance_x = transform.position.x + Random.Range(minRange, maxRange);
            float distance_z = transform.position.z + Random.Range(minRange, maxRange);

            WP = Instantiate(WaypointPrefab, new Vector3(distance_x, 0, distance_z), Quaternion.identity) as GameObject;

            wayP = true;
            move();
        }
    }

    void move()
    {
        animWalk();
        var rotation = Quaternion.LookRotation(WP.transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 3.0f * Time.deltaTime);
        transform.position = Vector3.MoveTowards(transform.position, WP.transform.position, enemySpeed * Time.deltaTime);
    }

    void OnTriggerEnter(Collider c)
    {
        if (c.gameObject.tag == "waypoint")
        {
            wayP = false;
            Destroy(WP);
        }
    }

    void animWalk()
    {
        anim.SetBool("isWalking", true);
        anim.SetBool("isAttacking", false);
        anim.SetBool("isIdle", false);
    }

    void animAttack()
    {
        anim.SetBool("isWalking", false);
        anim.SetBool("isAttacking", true);
        anim.SetBool("isIdle", false);
    }

    void animIdle()
    {
        anim.SetBool("isWalking", false);
        anim.SetBool("isAttacking", false);
        anim.SetBool("isIdle", true);
    }

thanks alot!
kelvin

Your animator is set to static, meaning there’s only one instance of that animator for every object that contains this script. Static is the way to define a Class variable, which is a variabile that is common for every instance of that class. When you have one object everything works fine, when you have several they interact with each others bools and mess everything… Any particular reason you set that to static?

1 Like

thanks! no not really. i got it from another script i made which is used for 1 npc only. thanks alot!