Thursday, April 28, 2016

EnemyAttack Script (Computer Aspect) (Close Attack)

EnemyAttack:

using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour
{
                public float timeBetweenAttacks = 0.5f;    // The time in seconds between each attack.
                public int attackDamage = 10;              // The amount of health taken away per attack.


                Animator m_animator;                       // Reference to the animator component.
                Animator m_lightAttack;                           // Reference to  the animator component.
                Animator m_DoNotAttack;                      // Reference to the animator component.
                GameObject m_player;                       // Reference to the player GameObject.
                //GameObject m_enemy;                        // Reference to the enemy GameObject.
                PlayerHealth m_playerHealth;               // Reference to the player's health.
                EnemyHealth m_enemyHealth;                  // Reference to this enemy's health.
                bool playerInRange;                         // Whether player is within the trigger collider and can be attacked.
               
float timer;                               // Timer for counting up to the next attack.


    void Awake ()
    {  

                                // Setting up the references.
        m_player = GameObject.FindGameObjectWithTag ("Player");
        m_playerHealth = m_player.GetComponent <PlayerHealth> ();
        m_enemyHealth = GetComponent<EnemyHealth>();
        m_animator = GetComponent <Animator> ();
        m_lightAttack = GetComponent <Animator> ();
        m_DoNotAttack = GetComponent <Animator> ();
    }


    void OnTriggerEnter (Collider other)
    {
                                // If the entering collider is the player...
                                if(other.gameObject == m_player)
        {
                                                // ... the player is in range.
                                                playerInRange = true;
        }
    
}


    void OnTriggerExit (Collider other)
    {
                                // If the exiting collider is the player...
                                if(other.gameObject == m_player)
        {
                                                // ... the player is no longer in range.
                                                playerInRange = false;
        }
    
}


    void Update ()
    {
       
                                // Add the time since Update was last called to the timer.
                                timer += Time.deltaTime;

                                // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
                                if (timer >= timeBetweenAttacks && playerInRange && m_enemyHealth.currentHealth > 0)
                                {
                                                Attack ();
                                }

                                else if (!playerInRange)
                                {
                                                DoNotAttack ();
                                }

                                // If the player has zero or less health...
                                if(m_playerHealth.currentHealth <= 0)
        {
                m_animator.SetTrigger ("PlayerDead");
                m_lightAttack.SetTrigger ("PlayerDead");
                                               
        }
    }


    void Attack ()
    {
                                // Reset the timer.
                                timer = 0f;

                                // If the player has health to lose...
                                if(m_playerHealth.currentHealth > 0)
        {
                                                // ... Attack the player.

                                                m_lightAttack.SetTrigger ("LightAttack");
        }
                                 
                }

                void DamagePlayer()
                {
                                //Damage the player.
                                m_playerHealth.TakeDamage (attackDamage);
                }

                void DoNotAttack ()
                {
                                m_DoNotAttack.SetBool ("DoNotAttack", false);
                }
}


No comments:

Post a Comment