Advertisement
har5452

Untitled

Feb 8th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI; // Required for UI elements
  5. [RequireComponent(typeof(AudioSource))]
  6. public class Chaser2 : MonoBehaviour {
  7.  
  8. public float speed = 20.0f;
  9. public float minDist = 0.2f;
  10. private float health = 100f;
  11. public Transform target;
  12. public bool isAttacking = false;
  13. private Rigidbody rb;
  14. public bool takeDamage;
  15. public ParticleSystem bloodParticles;
  16. public bool isInRange = false;
  17. public bool isDead = false;
  18. public float damageVal;
  19. public bool isEnraged;
  20. public AudioClip pickup;
  21.  
  22. public float attackRange = 15f;
  23. public float attackDamage = 10f;
  24. public float attackCooldown = 1f;
  25.  
  26. public bool PlayerLeft;
  27.  
  28. private AudioSource source;
  29. private Coroutine attackCoroutine;
  30.  
  31. void OnEnable() {
  32. health = 100f;
  33. }
  34.  
  35. void Awake() {
  36. source = GetComponent<AudioSource>();
  37. rb = GetComponent<Rigidbody>();
  38. }
  39.  
  40. void Start() {
  41. if (target == null) {
  42. GameObject player = GameObject.FindWithTag("Player");
  43. if (player != null) {
  44. target = player.transform;
  45. }
  46. }
  47.  
  48. if (bloodParticles == null) {
  49. bloodParticles = GetComponentInChildren<ParticleSystem>();
  50. }
  51. }
  52.  
  53. void Update() {
  54. if (!isDead) {
  55. // Always chase the player unless dead
  56. Chase();
  57.  
  58. // If the player enters the range and the enemy is not attacking, start attacking
  59. if (isInRange && !isAttacking) {
  60. StartAttacking();
  61. }
  62. // Stop attacking when the player exits the range
  63. else if (!isInRange && isAttacking) {
  64. StopAttacking();
  65. }
  66. }
  67. }
  68.  
  69. private void StartAttacking() {
  70. // Start the coroutine only if it's not already running
  71. if (attackCoroutine == null) {
  72. attackCoroutine = StartCoroutine(ShootAtPlayer());
  73. }
  74. }
  75.  
  76. private void StopAttacking() {
  77. if (attackCoroutine != null) {
  78. StopCoroutine(attackCoroutine);
  79. attackCoroutine = null;
  80. Debug.Log("Attack coroutine stopped.");
  81. }
  82. isAttacking = false;
  83. Debug.Log("isAttacking set to false.");
  84. }
  85.  
  86. private IEnumerator ShootAtPlayer() {
  87. int shotCount = 0; // Counter for the number of shots fired
  88. float timer = 0f; // Timer to track elapsed time
  89.  
  90. // Continue attacking as long as the player is in range and the enemy is not dead
  91. while (isInRange && !isDead) {
  92. // If enough time has passed for the cooldown, shoot again
  93. if (timer >= attackCooldown) {
  94. // Perform the attack (shoot the ray)
  95. Debug.Log("Shooting at player.");
  96. ShootRaycast();
  97.  
  98. // Reset the timer after shooting
  99. timer = 0f;
  100.  
  101. // Increment the shot count
  102. shotCount++;
  103. Debug.Log("Shot count: " + shotCount);
  104. }
  105.  
  106. // Increment the timer based on the time passed since the last frame
  107. timer += Time.deltaTime;
  108.  
  109. // Wait for the next frame before checking again
  110. yield return null;
  111. }
  112.  
  113. // Once out of range or dead, stop attacking and reset the state
  114. isAttacking = false;
  115. Debug.Log("Attack stopped, player out of range.");
  116. }
  117.  
  118. private void ShootRaycast() {
  119. if (target == null) return;
  120.  
  121. Vector3 direction = (target.position - transform.position).normalized;
  122. if (Physics.Raycast(transform.position, direction, out RaycastHit hit, attackRange)) {
  123. if (hit.transform.CompareTag("Player")) {
  124. playermovement.Damage(10);
  125. Debug.Log("Hit player! Damage dealt: " + attackDamage);
  126. }
  127. }
  128. Debug.DrawRay(transform.position, direction * attackRange, Color.red, 1f);
  129. }
  130.  
  131. private void Chase() {
  132. if (target == null) return;
  133.  
  134. Vector3 direction = (target.position - transform.position).normalized;
  135. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 5f);
  136.  
  137. float distance = Vector3.Distance(transform.position, target.position);
  138.  
  139. // ✅ Check if the player is in range using distance
  140. isInRange = distance <= attackRange;
  141.  
  142. // ✅ Move if too far, stop otherwise
  143. if (distance > minDist && !isAttacking) {
  144. rb.MovePosition(rb.position + direction * speed * Time.deltaTime);
  145. } else {
  146. rb.velocity = Vector3.zero;
  147. }
  148.  
  149. // ✅ Debug attack range visualization
  150. Debug.DrawRay(transform.position, direction * attackRange, Color.red, 1f);
  151. }
  152.  
  153.  
  154.  
  155. public void AddDamage(float damage) {
  156. if (isDead) return;
  157.  
  158. takeDamage = true;
  159. isEnraged = true;
  160. source.PlayOneShot(pickup);
  161. damageVal = damage;
  162. health -= damage;
  163. bloodParticles?.Play();
  164.  
  165. if (health <= 0) {
  166. Die();
  167. } else {
  168. StartCoroutine(ResetTakeDamageFlag());
  169. }
  170. }
  171.  
  172. private void Die() {
  173. isDead = true;
  174. isAttacking = false;
  175. rb.velocity = Vector3.zero;
  176. bloodParticles?.Play();
  177. StopAllCoroutines();
  178. }
  179.  
  180. private IEnumerator ResetTakeDamageFlag() {
  181. yield return new WaitForSeconds(0.1f);
  182. takeDamage = false;
  183. }
  184.  
  185. public void KillCharacter() {
  186. Destroy(gameObject);
  187. }
  188. }
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement