Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI; // Required for UI elements
- [RequireComponent(typeof(AudioSource))]
- public class Chaser2 : MonoBehaviour {
- public float speed = 20.0f;
- public float minDist = 0.2f;
- private float health = 100f;
- public Transform target;
- public bool isAttacking = false;
- private Rigidbody rb;
- public bool takeDamage;
- public ParticleSystem bloodParticles;
- public bool isInRange = false;
- public bool isDead = false;
- public float damageVal;
- public bool isEnraged;
- public AudioClip pickup;
- public float attackRange = 15f;
- public float attackDamage = 10f;
- public float attackCooldown = 1f;
- private AudioSource source;
- private Coroutine attackCoroutine;
- void OnEnable() {
- health = 100f;
- }
- void Awake() {
- source = GetComponent<AudioSource>();
- rb = GetComponent<Rigidbody>();
- }
- void Start() {
- if (target == null) {
- GameObject player = GameObject.FindWithTag("Player");
- if (player != null) {
- target = player.transform;
- }
- }
- if (bloodParticles == null) {
- bloodParticles = GetComponentInChildren<ParticleSystem>();
- }
- }
- void Update() {
- if (!isAttacking && !isDead) {
- Chase();
- }
- if (isInRange && !isAttacking && !isDead) {
- if (attackCoroutine == null) {
- attackCoroutine = StartCoroutine(ShootAtPlayer());
- }
- } else {
- if (attackCoroutine != null) {
- StopAttacking();
- }
- }
- }
- private void Chase() {
- if (target == null) return;
- Vector3 direction = (target.position - transform.position).normalized;
- transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * 5f);
- float distance = Vector3.Distance(transform.position, target.position);
- // ✅ Check if the player is in range using distance instead of OverlapSphere
- isInRange = distance <= attackRange;
- // ✅ Move if too far, stop otherwise
- if (distance > minDist && !isAttacking) {
- rb.MovePosition(rb.position + direction * speed * Time.deltaTime);
- } else {
- rb.velocity = Vector3.zero;
- }
- // ✅ Debug attack range visualization
- Debug.DrawRay(transform.position, direction * attackRange, Color.red, 1f);
- }
- private IEnumerator ShootAtPlayer() {
- ShootRaycast();
- yield return new WaitForSeconds(attackCooldown);
- isAttacking = false;
- }
- private void ShootRaycast() {
- if (target == null) return;
- Vector3 direction = (target.position - transform.position).normalized;
- if (Physics.Raycast(transform.position, direction, out RaycastHit hit, attackRange)) {
- if (hit.transform.CompareTag("Player")) {
- playermovement.Damage(10);
- Debug.Log("Hit player! Damage dealt: " + attackDamage);
- }
- }
- Debug.DrawRay(transform.position, direction * attackRange, Color.red, 1f);
- }
- private void StopAttacking() {
- if (attackCoroutine != null) {
- StopCoroutine(attackCoroutine);
- attackCoroutine = null;
- Debug.Log("Attack coroutine stopped.");
- }
- isAttacking = false;
- Debug.Log("isAttacking set to false.");
- }
- public void AddDamage(float damage) {
- if (isDead) return;
- takeDamage = true;
- isEnraged = true;
- source.PlayOneShot(pickup);
- damageVal = damage;
- health -= damage;
- bloodParticles?.Play();
- if (health <= 0) {
- Die();
- } else {
- StartCoroutine(ResetTakeDamageFlag());
- }
- }
- private void Die() {
- isDead = true;
- isAttacking = false;
- rb.velocity = Vector3.zero;
- bloodParticles?.Play();
- StopAllCoroutines();
- }
- private IEnumerator ResetTakeDamageFlag() {
- yield return new WaitForSeconds(0.1f);
- takeDamage = false;
- }
- public void KillCharacter() {
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement