Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class EnemyHealth : MonoBehaviour
  4. {
  5. public int startingHealth = 100;
  6. public int currentHealth;
  7. public float sinkSpeed = 2.5f;
  8. public int scoreValue = 10;
  9. public AudioClip deathClip;
  10.  
  11.  
  12. Animator anim;
  13. AudioSource enemyAudio;
  14. ParticleSystem hitParticles;
  15. CapsuleCollider capsuleCollider;
  16. bool isDead;
  17. bool isSinking;
  18.  
  19.  
  20. void Awake ()
  21. {
  22. anim = GetComponent <Animator> ();
  23. enemyAudio = GetComponent <AudioSource> ();
  24. hitParticles = GetComponentInChildren <ParticleSystem> ();
  25. capsuleCollider = GetComponent <CapsuleCollider> ();
  26.  
  27. currentHealth = startingHealth;
  28. }
  29.  
  30.  
  31. void Update ()
  32. {
  33. if(isSinking)
  34. {
  35. transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
  36. }
  37. }
  38.  
  39.  
  40. public void TakeDamage (int amount, Vector3 hitPoint)
  41. {
  42. if(isDead)
  43. return;
  44.  
  45. enemyAudio.Play ();
  46.  
  47. currentHealth -= amount;
  48.  
  49. hitParticles.transform.position = hitPoint;
  50. hitParticles.Play();
  51.  
  52. if(currentHealth <= 0)
  53. {
  54. Death ();
  55. }
  56. }
  57.  
  58.  
  59. void Death ()
  60. {
  61. isDead = true;
  62.  
  63. capsuleCollider.isTrigger = true;
  64.  
  65. anim.SetTrigger ("Dead");
  66.  
  67. enemyAudio.clip = deathClip;
  68. enemyAudio.Play ();
  69. }
  70.  
  71.  
  72. public void StartSinking()
  73. {
  74. // Find and disable the Nav Mesh Agent.
  75. GetComponent<NavMeshAgent>().enabled = false;
  76.  
  77. // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
  78. GetComponent<Rigidbody>().isKinematic = true;
  79.  
  80. // The enemy should no sink.
  81. isSinking = true;
  82.  
  83. // Increase the score by the enemy's score value.
  84. ScoreManager.score += scoreValue;
  85.  
  86. // After 2 seconds destory the enemy.
  87. Destroy(gameObject, 2f);
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement