Advertisement
Guest User

EnemyHealth.cs

a guest
Oct 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. using UnityEngine;
  2. //added
  3. using System.Collections;
  4.  
  5. namespace CompleteProject
  6. {
  7.     public class EnemyHealth : MonoBehaviour
  8.     {
  9.         public int startingHealth = 100;            // The amount of health the enemy starts the game with.
  10.         public int currentHealth;                   // The current health the enemy has.
  11.         public float sinkSpeed = 2.5f;              // The speed at which the enemy sinks through the floor when dead.
  12.         public int scoreValue = 10;                 // The amount added to the player's score when the enemy dies.
  13.         public AudioClip deathClip;                 // The sound to play when the enemy dies.
  14.  
  15.         //added
  16.         GameObject player;
  17.  
  18.         Animator anim;                              // Reference to the animator.
  19.         AudioSource enemyAudio;                     // Reference to the audio source.
  20.         ParticleSystem hitParticles;                // Reference to the particle system that plays when the enemy is damaged.
  21.         CapsuleCollider capsuleCollider;            // Reference to the capsule collider.
  22.         bool isDead;                                // Whether the enemy is dead.
  23.         bool isSinking;                             // Whether the enemy has started sinking through the floor.
  24.  
  25.  
  26.         void Awake ()
  27.         {
  28.             player = GameObject.FindGameObjectWithTag ("Player");
  29.             // Setting up the references.
  30.             anim = GetComponent <Animator> ();
  31.             enemyAudio = GetComponent <AudioSource> ();
  32.             hitParticles = GetComponentInChildren <ParticleSystem> ();
  33.             capsuleCollider = GetComponent <CapsuleCollider> ();
  34.  
  35.             // Setting the current health when the enemy first spawns.
  36.             currentHealth = startingHealth;
  37.         }
  38.  
  39.  
  40.         void Update ()
  41.         {
  42.             if (Input.GetKey (KeyCode.Space)) {
  43.                 Explosion ();
  44.             }
  45.  
  46.             // If the enemy should be sinking...
  47.             if(isSinking)
  48.             {
  49.                 // ... move the enemy down by the sinkSpeed per second.
  50.                 transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
  51.             }
  52.  
  53.         }
  54.  
  55.  
  56.         public void TakeDamage (int amount, Vector3 hitPoint)
  57.         {
  58.             // If the enemy is dead...
  59.             if(isDead)
  60.                 // ... no need to take damage so exit the function.
  61.                 return;
  62.  
  63.             // Play the hurt sound effect.
  64.             enemyAudio.Play ();
  65.  
  66.             // Reduce the current health by the amount of damage sustained.
  67.             currentHealth -= amount;
  68.            
  69.             // Set the position of the particle system to where the hit was sustained.
  70.             hitParticles.transform.position = hitPoint;
  71.  
  72.             // And play the particles.
  73.             hitParticles.Play();
  74.  
  75.             // If the current health is less than or equal to zero...
  76.             if(currentHealth <= 0)
  77.             {
  78.                 // ... the enemy is dead.
  79.                 Death ();
  80.             }
  81.  
  82.  
  83.         }
  84.  
  85.         public void Explosion ()
  86.         {
  87.             Vector3 enemyPos = gameObject.transform.position;
  88.             Vector3 playerPos = player.transform.position;
  89.  
  90.             float dist = Vector3.Distance(enemyPos, playerPos);
  91.  
  92.             if (dist < 5) {
  93.                 print (playerPos);
  94.                 Death ();
  95.             }
  96.         }
  97.  
  98.  
  99.         void Death ()
  100.         {
  101.             // The enemy is dead.
  102.             isDead = true;
  103.  
  104.             // Turn the collider into a trigger so shots can pass through it.
  105.             capsuleCollider.isTrigger = true;
  106.  
  107.             // Tell the animator that the enemy is dead.
  108.             anim.SetTrigger ("Dead");
  109.  
  110.             // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
  111.             enemyAudio.clip = deathClip;
  112.             enemyAudio.Play ();
  113.         }
  114.  
  115.  
  116.         public void StartSinking ()
  117.         {
  118.             // Find and disable the Nav Mesh Agent.
  119.             GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;
  120.  
  121.             // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
  122.             GetComponent <Rigidbody> ().isKinematic = true;
  123.  
  124.             // The enemy should no sink.
  125.             isSinking = true;
  126.  
  127.             // Increase the score by the enemy's score value.
  128.             ScoreManager.score += scoreValue;
  129.  
  130.             // After 2 seconds destory the enemy.
  131.             Destroy (gameObject, 2f);
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement