using System.Collections; using System.Collections.Generic; using UnityEngine; using static PlayerController; public class PlayerAttack : MonoBehaviour { Animator animator; PlayerController controller; public float attackTime = 0.5f; // °ø°Ý ÈÄ Äð´Ù¿î public float attackCoolTime = 0.5f; private int attackCount = 0; // ÇöÀç °ø°Ý Ƚ¼ö private int attackIdx = 0; // °ø°Ý ¾Ö´Ï¸ÞÀÌ¼Ç À妽º (0,1 ¹Ýº¹) private bool canAttack = true; private void Start() { controller = GetComponent(); animator = GetComponentInChildren(); PlayManager.Instance.leftMouve += Attack; Debug.Log(123); } void Attack() { if(!canAttack || controller.playerState == PlayerState.Skill) return; controller.playerState = PlayerState.Attack; attackCount++; animator.SetInteger("AttackIdx", attackIdx); attackIdx = (attackIdx + 1) % 2; AttackRot(); animator.SetFloat("idleState", controller.idleState); if(coroutine != null) { StopCoroutine(coroutine); coroutine = StartCoroutine(AttackStopCoroutin()); } else { coroutine = StartCoroutine(AttackStopCoroutin()); } if (attackCount >= 3) { StartCoroutine(AttackCooldown()); // ¸¶Áö¸· °ø°ÝÀ̸é Äð´Ù¿î ½ÇÇà } } Coroutine coroutine; IEnumerator AttackStopCoroutin() { yield return new WaitForSeconds(attackTime); AttackStop(); } private void AttackStop() { attackCount = 0; controller.playerState = PlayerState.Idle; animator.SetInteger("AttackIdx", -1); } public void AttackInitialized() { attackCount = 0; } IEnumerator AttackCooldown() { canAttack = false; yield return new WaitForSeconds(attackCoolTime); 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("AttackState", 1); controller.idleState = 1f; } else { if (mouseScreenPos.y > playerScreenPos.y) { animator.SetFloat("AttackState", 0.5f); controller.idleState = 2f / 3f; } else { animator.SetFloat("AttackState", 0); controller.idleState = 1f / 3f; } } } }