using UnityEngine; public class EnemyAI : MonoBehaviour { private InputHandler input; public Transform target; public Transform rotationTarget; private Transform hip; public Vector3 randomOffset; private PlayerDeath death; private PlayerDeath ownDeath; private float counter; private float counter2 = 1f; private float deadCounter; private bool hasDespawned; private void Start() { input = GetComponent(); hip = GetComponentInChildren().transform; death = target.root.GetComponent(); ownDeath = GetComponent(); } private void Update() { if (ownDeath.dead) { deadCounter += Time.deltaTime; if (deadCounter > 5f && !hasDespawned) { Despawn(); } if (deadCounter > 8f) { Object.Destroy(base.gameObject); } } else { Logic(); } } public bool IsDead() { return ownDeath.dead; } private void Despawn() { hasDespawned = true; Rigidbody[] componentsInChildren = GetComponentsInChildren(); Collider[] componentsInChildren2 = GetComponentsInChildren(); MonoBehaviour[] componentsInChildren3 = GetComponentsInChildren(); for (int i = 0; i < componentsInChildren.Length; i++) { componentsInChildren[i].drag = 5f; componentsInChildren[i].angularDrag = 5f; } for (int j = 0; j < componentsInChildren2.Length; j++) { componentsInChildren2[j].enabled = false; } for (int k = 0; k < componentsInChildren3.Length; k++) { if (componentsInChildren3[k].GetType() != GetType()) { componentsInChildren3[k].enabled = false; } } } private void Logic() { counter2 -= Time.deltaTime; if (counter2 < 0f) { counter2 = Random.Range(1f, 4f); randomOffset = new Vector3(Random.Range(-1f, 1f), 0f, Random.Range(-1f, 1f)); } float num = Vector3.Distance(target.position, hip.position); input.inputMovementDirection = (target.position + randomOffset * num * 0.7f - hip.position).normalized; rotationTarget.rotation = Quaternion.LookRotation(input.inputMovementDirection); input.isSpringting = true; counter += Time.deltaTime; if (num < 0.7f && counter > 0.3f) { death.TakeDamage(15f * (target.position - hip.position).normalized, Vector3.zero); counter = 0f; } } }