diff options
author | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
commit | 6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch) | |
tree | b38119d2acf0a982cb67e381f146924b9bfc3b3f /EnemyAI.cs |
+init
Diffstat (limited to 'EnemyAI.cs')
-rw-r--r-- | EnemyAI.cs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/EnemyAI.cs b/EnemyAI.cs new file mode 100644 index 0000000..7337bbe --- /dev/null +++ b/EnemyAI.cs @@ -0,0 +1,103 @@ +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<InputHandler>(); + hip = GetComponentInChildren<Hip>().transform; + death = target.root.GetComponent<PlayerDeath>(); + ownDeath = GetComponent<PlayerDeath>(); + } + + 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<Rigidbody>(); + Collider[] componentsInChildren2 = GetComponentsInChildren<Collider>(); + MonoBehaviour[] componentsInChildren3 = GetComponentsInChildren<MonoBehaviour>(); + 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; + } + } +} |