Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BulletEnemy : MonoBehaviour
  6. {
  7. TroopsHealth currentHealth;
  8. TroopsHealth realDamage;
  9.  
  10. private Transform target;
  11.  
  12. public float speed = 70f;
  13.  
  14. public int damage = 50;
  15.  
  16. public float armor = 1;
  17.  
  18. public float explosionRadius = 0f;
  19. public GameObject impactEffect;
  20.  
  21. public void Seek(Transform _target)
  22. {
  23. target = _target;
  24. }
  25.  
  26. void Update()
  27. {
  28. if (target == null)
  29. {
  30. Destroy(gameObject);
  31. return;
  32. }
  33.  
  34. Vector3 dir = target.position - transform.position;
  35. float distanceThisFrame = speed * Time.deltaTime;
  36.  
  37. if (dir.magnitude <= distanceThisFrame)
  38. {
  39. HitTarget();
  40. return;
  41. }
  42.  
  43. transform.Translate(dir.normalized * distanceThisFrame, Space.World);
  44. transform.LookAt(target);
  45. }
  46.  
  47. void HitTarget()
  48. {
  49. GameObject effectIns = (GameObject)Instantiate(impactEffect, transform.position, transform.rotation);
  50. Destroy(effectIns, 2f);
  51.  
  52. if (explosionRadius > 0f)
  53. {
  54. Explode();
  55. }
  56. else
  57. {
  58. Damage(target);
  59. }
  60.  
  61. Destroy(gameObject);
  62. }
  63.  
  64. void Explode()
  65. {
  66. Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
  67. foreach (Collider collider in colliders)
  68. {
  69. if (collider.tag == "Troops")
  70. {
  71. Damage(collider.transform);
  72. }
  73. }
  74. }
  75.  
  76. void Damage(Transform troops)
  77. {
  78.  
  79. TroopsHealth e = troops.GetComponent<TroopsHealth>();
  80.  
  81. if (e != null)
  82. {
  83. e.TakeDamage(damage);
  84. }
  85. }
  86.  
  87. public void TakeDamage(float amount)
  88. {
  89. var realDamage = amount - amount * .03f * armor;
  90. realDamage = Mathf.Max(1, realDamage);
  91. currentHealth -= realDamage;
  92. }
  93.  
  94. void OnDrawGizmosSelected()
  95. {
  96. Gizmos.color = Color.red;
  97. Gizmos.DrawWireSphere(transform.position, explosionRadius);
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement