using System.Collections; using UnityEngine; using static PlayerController; public class PlayerAttack : MonoBehaviour { Animator animator; PlayerController controller; public float attackTime = 0.5f; // °ø°Ý ÈÄ Äð´Ù¿î private int attackCount = 0; // ÇöÀç °ø°Ý Ƚ¼ö private float lastAttackTime; private bool canAttack = true; private int attackIdx = 0; // °ø°Ý ¾Ö´Ï¸ÞÀÌ¼Ç À妽º (0,1 ¹Ýº¹) private string[] anim_Strings = { "AttackIdx", "AttackState", "idleState" }; private void Start() { controller = GetComponent(); animator = GetComponentInChildren(); PlayManager.Instance.leftMouve += Attack; } private void Update() { if (controller.playerState == PlayerState.Dash) return; if (attackCount > 0 && Time.time - lastAttackTime >= attackTime) { AttackStop(); // ÀϹÝÀûÀÎ °æ¿ì´Â ±×³É AttackStop() } } void Attack() { if(!canAttack) return; controller.playerState = PlayerState.Attack; lastAttackTime = Time.time; attackCount++; Debug.Log($"Attack Count: {attackCount}, Attack Index: {attackIdx}"); animator.SetInteger(anim_Strings[0], attackIdx); attackIdx = (attackIdx + 1) % 2; AttackRot(); animator.SetFloat(anim_Strings[1], controller.idleState); if (attackCount >= 3) { StartCoroutine(AttackCooldown()); // ¸¶Áö¸· °ø°ÝÀ̸é Äð´Ù¿î ½ÇÇà } } public void AttackStop() { attackCount = 0; controller.playerState = PlayerState.Idle; animator.SetInteger(anim_Strings[0], -1); } IEnumerator AttackCooldown() { canAttack = false; yield return new WaitForSeconds(attackTime); AttackStop(); // ¸¶Áö¸· °ø°Ý ÈÄ Stop canAttack = true; } private void AttackRot() { Vector3 playerScreenPos = Camera.main.WorldToScreenPoint(transform.position); Vector3 mouseScreenPos = Input.mousePosition; bool isLeft = mouseScreenPos.x < playerScreenPos.x; if (Mathf.Abs(mouseScreenPos.x - playerScreenPos.x) > Mathf.Abs(mouseScreenPos.y - playerScreenPos.y)) { transform.GetChild(0).localEulerAngles = isLeft ? new Vector3(0, 180, 0) : new Vector3(0, 0, 0); animator.SetFloat(anim_Strings[2], 1); controller.idleState = 1f; } else { if (mouseScreenPos.y > playerScreenPos.y) { animator.SetFloat(anim_Strings[2], 0.5f); controller.idleState = 2f / 3f; } else { animator.SetFloat(anim_Strings[2], 0); controller.idleState = 1f / 3f; } } } }