Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class EnemyHealth : MonoBehaviour
- {
- public int startingHealth = 100;
- public int currentHealth;
- public float sinkSpeed = 2.5f;
- public int scoreValue = 10;
- public AudioClip deathClip;
- Animator anim;
- AudioSource enemyAudio;
- ParticleSystem hitParticles;
- CapsuleCollider capsuleCollider;
- bool isDead;
- bool isSinking;
- void Awake ()
- {
- anim = GetComponent <Animator> ();
- enemyAudio = GetComponent <AudioSource> ();
- hitParticles = GetComponentInChildren <ParticleSystem> ();
- capsuleCollider = GetComponent <CapsuleCollider> ();
- currentHealth = startingHealth;
- }
- void Update ()
- {
- if(isSinking)
- {
- transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
- }
- }
- public void TakeDamage (int amount, Vector3 hitPoint)
- {
- if(isDead)
- return;
- enemyAudio.Play ();
- currentHealth -= amount;
- hitParticles.transform.position = hitPoint;
- hitParticles.Play();
- if(currentHealth <= 0)
- {
- Death ();
- }
- }
- void Death ()
- {
- isDead = true;
- capsuleCollider.isTrigger = true;
- anim.SetTrigger ("Dead");
- enemyAudio.clip = deathClip;
- enemyAudio.Play ();
- }
- public void StartSinking()
- {
- // Find and disable the Nav Mesh Agent.
- GetComponent<NavMeshAgent>().enabled = false;
- // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
- GetComponent<Rigidbody>().isKinematic = true;
- // The enemy should no sink.
- isSinking = true;
- // Increase the score by the enemy's score value.
- ScoreManager.score += scoreValue;
- // After 2 seconds destory the enemy.
- Destroy(gameObject, 2f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement