Untitled
using System.Collections; using System.Collections.Generic; using UnityEngine; public class HatSamuraiAI : MonoBehaviour { #region Public Variables public Transform raycast; public LayerMask raycastMask; public float raycastLength; public float attackDistance; public float moveSpeed; public float timer; public GameObject downSlashWarning; #endregion #region Private Variables private RaycastHit2D hit; private GameObject target; private Animator anim; private float distance; private bool attackMode; private bool inRange; private bool cooling; private float intTimer; private string[] attackAnimations = { "DownSlash", "SideSlash", "UpSlash" }; private bool canAttack; #endregion void Awake() { intTimer = timer; anim = GetComponent<Animator>(); } // Update is called once per frame void Update() { if (inRange) { hit = Physics2D.Raycast(raycast.position, Vector2.left, raycastLength, raycastMask); RaycastDebugger(); } if (hit.collider != null) { EnemyLogic(); } else if (hit.collider == null) { inRange = false; } if (inRange == false) { anim.SetBool("isRunning", false); StopAttack(); } } void OnTriggerEnter2D(Collider2D trig) { if (trig.gameObject.tag == "Player") { target = trig.gameObject; inRange = true; } } private void EnemyLogic() { distance = Vector2.Distance(transform.position, target.transform.position); if (distance > attackDistance) { Move(); StopAttack(); } else if (attackDistance >= distance && cooling == false) { canAttack = true; } if (canAttack) { RandomlyChooseAttack(); } if (cooling) { CoolDown(); anim.SetBool("DownSlash", false); anim.SetBool("SideSlash", false); anim.SetBool("UpSlash", false); } } void Move() { anim.SetBool("isRunning", true); if (!anim.GetCurrentAnimatorStateInfo(0).IsName("EnemySideSlash")) { Vector2 targetPosition = new Vector2(target.transform.position.x, transform.position.y); transform.position = Vector2.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime); } } private void RandomlyChooseAttack() { canAttack = false; // Randomly select an attack animation from the array string randomAttack = attackAnimations[Random.Range(0, attackAnimations.Length)]; Debug.Log(randomAttack); // Call the selected attack animation method switch (randomAttack) { case "DownSlash": DownSlash(); break; case "SideSlash": SideSlash(); break; case "UpSlash": UpSlash(); break; // Add more cases if needed default: Debug.LogError("Invalid attack animation: " + randomAttack); break; } } void DownSlash() { downSlashWarning.SetActive(true); timer = intTimer; attackMode = true; anim.SetBool("isRunning", false); anim.SetBool("DownSlash", true); } void SideSlash() { timer = intTimer; attackMode = true; anim.SetBool("isRunning", false); anim.SetBool("SideSlash", true); } void UpSlash() { timer = intTimer; attackMode = true; anim.SetBool("isRunning", false); anim.SetBool("UpSlash", true); } void CoolDown() { timer -= Time.deltaTime; if (timer < 0 && cooling && attackMode) { cooling = false; canAttack = false; timer = intTimer; } } void StopAttack() { cooling = false; attackMode = false; anim.SetBool("DownSlash", false); anim.SetBool("SideSlash", false); anim.SetBool("UpSlash", false); } void RaycastDebugger() { if (distance > attackDistance) { Debug.DrawRay(raycast.position, Vector2.left * raycastLength, Color.red); } else if (attackDistance > distance) { Debug.DrawRay(raycast.position, Vector2.left * raycastLength, Color.green); } } public void TriggerCooling() { cooling = true; canAttack = true; } }
Leave a Comment