Mecanim Over and Over Again

hi again, after i get my player to work properly with mecanim i try it with my enemy, the enemy stay in idle, run when in range of chase player but when it get in attack range, it gives the damage has it should but instead of playing attack animation it keep running at same spot… i check the animator window and i can see this…

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

public class Enemy : HumanoidControl
{
    Player player;

    public float chasingRange;

    bool blockEnemy;

    bool mouseOver;


    NavMeshAgent navAgent;

    private Animator anim;
   
    private bool running;
   
    private bool attacking;



    void Start ()
    {
        player = Player.player;

        navAgent = GetComponent<NavMeshAgent>();

        mouseOver = false;

        anim = GetComponent<Animator>();
    }



    void Update ()
    {
        Attack();

        FollowPlayer();
    }



    void FollowPlayer()
    {
        if (IsInRange (chasingRange))
        {
            running = true;

            attacking = false;

            navAgent.SetDestination (player.transform.position);

            navAgent.Resume();
        }
        else
        {
            //change this to return initial position
   
            navAgent.SetDestination(transform.position);
        }

        anim.SetBool ("IsRunning", running);
    }



    void OnMouseOver()
    {
        Player.opponent = transform;
    }



    protected override void Attack()
    {
        if (IsInRange(attackRange))
        {
            if(!blockEnemy)
            {
                player.GetHit(damage);

                attacking = true;

                anim.SetTrigger("IsAttacking");

                blockEnemy = true;

                Invoke("UnBlock", attackSpeed);
            }

            anim.SetBool ("IsAttacking", attacking);
        }

        if(Vector3.Distance(player.transform.position, transform.position) < attackRange)
        {
            attacking = true;
        }
        else
        {
            attacking = false;
        }

        anim.SetBool ("IsAttacking", attacking);
    }



    void UnBlock()
    {
        blockEnemy = false;
    }


    bool IsInRange(float range)
    {
        if(Vector3.Distance (player.transform.position, transform.position) < range)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

It looks like it may just be an issue with your Update and IsInRange checks. Since your update always calls both Attack() and FollowPlayer()… anytime your Attack is in range, your FollowPlayer will also be in range(I assume follow has a bigger range than attack). From a quick look, it seems like calling FollowPlayer everytime after Attack in Update method is just overwriting the Attack method’s animation setup.

Try setting an extra conditional or altering something, so that if Attack is in range, it doesn’t overwrite it with the following FollowPlayer call in Update. Maybe create an extra method that runs the IsInRange checks with an if/else like so:

if (IsInRange(attackRange))
    Attack();
else if(IsInRange(chasingRange))
    FollowPlayer();

… and alter those methods to match it.

Just a guess, but I don’t really understand what your UnBlock and all that are doing, so I could be off. But, that’s what I’d run some checks on.

1 Like

The unblock works for the cooldown between attacks… i think its not a good method but work… i try wt i tell me but the problem persist… :s