Advertisement
har5452

Untitled

Feb 8th, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 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. private AudioSource source;
  27. private Coroutine attackCoroutine;
  28.  
  29. void OnEnable() {
  30. health = 100f;
  31. }
  32.  
  33. void Awake() {
  34. source = GetComponent<AudioSource>();
  35. rb = GetComponent<Rigidbody>();
  36. }
  37.  
  38. void Start() {
  39. if (target == null) {
  40. GameObject player = GameObject.FindWithTag("Player");
  41. if (player != null) {
  42. target = player.transform;
  43. }
  44. }
  45.  
  46. if (bloodParticles == null) {
  47. bloodParticles = GetComponentInChildren<ParticleSystem>();
  48. }
  49. }
  50.  
  51. void Update() {
  52. if (!isAttacking && !isDead) {
  53. Chase();
  54. }
  55.  
  56. if (isInRange && !isAttacking && !isDead) {
  57. if (attackCoroutine == null) {
  58. attackCoroutine = StartCoroutine(ShootAtPlayer());
  59. }
  60. } else {
  61. if (attackCoroutine != null) {
  62. StopAttacking();
  63. }
  64. }
  65. }
  66.  
  67.  
  68. private void Chase() {
  69. if (target == null) return;
  70.  
  71. Vector3 direction = (target.position - transform.position).normalized;
  72. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 5f);
  73.  
  74. float distance = Vector3.Distance(transform.position, target.position);
  75.  
  76. // ✅ Check if the player is in range using distance instead of OverlapSphere
  77. isInRange = distance <= attackRange;
  78.  
  79. // ✅ Move if too far, stop otherwise
  80. if (distance > minDist && !isAttacking) {
  81. rb.MovePosition(rb.position + direction * speed * Time.deltaTime);
  82. } else {
  83. rb.velocity = Vector3.zero;
  84. }
  85.  
  86. // ✅ Debug attack range visualization
  87. Debug.DrawRay(transform.position, direction * attackRange, Color.red, 1f);
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. private IEnumerator ShootAtPlayer() {
  95. ShootRaycast();
  96. yield return new WaitForSeconds(attackCooldown);
  97. isAttacking = false;
  98. }
  99.  
  100. private void ShootRaycast() {
  101. if (target == null) return;
  102.  
  103. Vector3 direction = (target.position - transform.position).normalized;
  104. if (Physics.Raycast(transform.position, direction, out RaycastHit hit, attackRange)) {
  105. if (hit.transform.CompareTag("Player")) {
  106. playermovement.Damage(10);
  107. Debug.Log("Hit player! Damage dealt: " + attackDamage);
  108. }
  109. }
  110. Debug.DrawRay(transform.position, direction * attackRange, Color.red, 1f);
  111. }
  112.  
  113. private void StopAttacking() {
  114. if (attackCoroutine != null) {
  115. StopCoroutine(attackCoroutine);
  116. attackCoroutine = null;
  117. Debug.Log("Attack coroutine stopped.");
  118. }
  119. isAttacking = false;
  120. Debug.Log("isAttacking set to false.");
  121. }
  122.  
  123.  
  124.  
  125. public void AddDamage(float damage) {
  126. if (isDead) return;
  127.  
  128. takeDamage = true;
  129. isEnraged = true;
  130. source.PlayOneShot(pickup);
  131. damageVal = damage;
  132. health -= damage;
  133. bloodParticles?.Play();
  134.  
  135. if (health <= 0) {
  136. Die();
  137. } else {
  138. StartCoroutine(ResetTakeDamageFlag());
  139. }
  140. }
  141.  
  142. private void Die() {
  143. isDead = true;
  144. isAttacking = false;
  145. rb.velocity = Vector3.zero;
  146. bloodParticles?.Play();
  147. StopAllCoroutines();
  148. }
  149.  
  150. private IEnumerator ResetTakeDamageFlag() {
  151. yield return new WaitForSeconds(0.1f);
  152. takeDamage = false;
  153. }
  154.  
  155. public void KillCharacter() {
  156. Destroy(gameObject);
  157. }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement