Exemple #1
0
    // Update is called once per frame
    public virtual void Update()
    {
        AnimationState();

        // Distance from player and enemy
        float distance = Vector3.Distance(target.position, transform.position);

        if (distance <= lookRadius)
        {
            agent.SetDestination(target.position);

            animationState = State.Running;

            if (soundPlayed == false)
            {
                AudioManager.instance.Play(soundName);
                soundPlayed = true;
            }

            // Call attack method
            Attack();

            if (distance <= agent.stoppingDistance)
            {
                // Face the player
                FaceTarget();
            }
        }
        else
        {
            animationState = State.Idle; // Return to Idle animation if outside of radius
        }

        if (EnemyHealth.CurrentHealth < 0)
        {
            animationState = State.Death;

            if (singleDeathCheck == false)
            {
                singleDeathCheck = true;

                AudioManager.instance.Play("EnemyDying");
                agent.isStopped = true; // Stop the navmesh when the enemy is dead

                // Reference to the EnemyManager object with the enemy gameObject passed in
                FindObjectOfType <EnemyManager>().KilledEnemy(gameObject);

                enemyHealth.Die(deathTime);
                Debug.Log("Enemy dead");
            }
        }
    }