Tuesday, May 3, 2016

EnemyHealth Script

using UnityEngine;

public class EnemyHealth : MonoBehaviour
{
                public int startingHealth = 100;       // The amount of health the enemy starts the game with.
                public int currentHealth;              // The current health the enemy has.
    //public float sinkSpeed = 2.5f;
                public int scoreValue = 10;            // The amount added to the player's score when the enemy dies.
    //public AudioClip deathClip;


                Animator m_animator;                    // Reference to the animator.
    //AudioSource m_enemyAudio;
                ParticleSystem m_hitParticles;          // Reference to the particle system that plays when the enemy is damaged.
                CapsuleCollider m_capsuleCollider;      // Reference to the capsule collider.
                bool isDead;                            // Whether the enemy is dead.
    //bool isSinking;


    void Awake ()
    {
                                // Setting up the references.
        m_animator = GetComponent <Animator> ();
        m_hitParticles = GetComponentInChildren <ParticleSystem> ();
        m_capsuleCollider = GetComponent <CapsuleCollider> ();

                       // Setting the current health when the enemy first spawns
                          currentHealth = startingHealth;
    }




    public void TakeDamage (int amount, Vector3 hitPoint)
    {
                                // If the enemy is dead...
                                if(isDead)
                                                // ... no need to take damage so exit the function.
                                                return;

                                //m_enemyAudio.Play ();

                                // Reduce the current health by the amount of damage sustained.
        currentHealth -= amount;
           
                                // Set the position of the particle system to where the hit was sustained.
                                m_hitParticles.transform.position = hitPoint;
       
                                // And play the particles.
                                m_hitParticles.Play();

                                // If the current health is less than or equal to zero...
                                if(currentHealth <= 0)
        {
                                                // ... the enemy is dead.
                                                Death ();
        }
    }


    void Death ()
    {
                                // ... the enemy is dead.
                                isDead = true;

                                // Turn the collider into a trigger so shots can pass through it.
                                m_capsuleCollider.isTrigger = true;

                                // Tell the animator that the enemy is dead.
                                m_animator.SetTrigger ("Dead");

        //m_enemyAudio.clip = deathClip;
        //m_enemyAudio.Play ();
                                ScoreManager.score += scoreValue;
    }



}

No comments:

Post a Comment