1 분 소요

Intro

지난번 만들었던 콤보공격에 문제점이 많아 밤새 해결방법을 찾으려고 노력했습니다.

  먼저 다음과 같은 문제점들이 있었습니다.

  1. 첫공격이후 추가입력을 통해 연계공격이 나가는중에 또다시 공격키를 입력하면 공격함수가 이중으로 실행되는점
  2. 애니메이션과 실제 공격시점에 오차가있었던점.

2번 문제같은경우, 단순히 콜라이더 활성/비활성화 시점만 손보면 되는것이었지만

1번 문제때문에 시간이 많이 소요되었습니다.

수정된코드

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Attack : MonoBehaviour
{
    [SerializeField] private PlayerInput playerInput;

    [SerializeField] private BoxCollider leftHandCollider;               
    [SerializeField] private BoxCollider rightHandCollider;

    private Animator anim;

    readonly private int hashAttackType = Animator.StringToHash("AttackType");
    readonly private int hashComboCount = Animator.StringToHash("ComboCount");
    readonly private int hashAttackTrigger = Animator.StringToHash("Attack");
    
    
    private bool hasEquipedWeapon = false;      // 무기 장착여부(현재는 기본 false)
    public bool isAttack = false;              // 공격중인지 여부
    private bool isComboAllowed = false;        // 콤보공격 가능여부
   
    private int comboCount = 0;                 // 콤보공격 카운트
    private float comboResetTime = 1.0f;
    // AttackType 설정
    public int AttackType
    {
        get => anim.GetInteger(hashAttackType);
        set => anim.SetInteger(hashAttackType, value);
    }

    private void Awake()
    {
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        BaseAttack();
    }

    // 기본공격
    private void BaseAttack()
    {
        // 무기가 없을 때(주먹공격)
        if(hasEquipedWeapon == false)
        {
            AttackType = 0;
        }
     
        if(playerInput.attackKeydown && !isAttack)
        {
            StartCoroutine(BaseAttackPunch());
        }
    }

    // 기본 주먹공격
    private IEnumerator BaseAttackPunch()
    {
        isAttack = true;
        comboCount++;

        anim.SetInteger(hashAttackType, AttackType);
        anim.SetInteger(hashComboCount, comboCount);
        anim.SetTrigger(hashAttackTrigger);

        // 공격1타
        if (comboCount == 1)
        {
            leftHandCollider.enabled = true;
            yield return new WaitForSeconds(0.5f);

            // 추가 콤보입력감지
            float timer = 0;
            while (timer < 0.5f)
            {
                if (playerInput.attackKeydown)
                {
                    comboCount++;
                    isComboAllowed = true;
                    break;
                }
                timer += Time.deltaTime;
                yield return null;
            }
            leftHandCollider.enabled = false;
        }

        // 추가 콤보입력이 발생했을 때
        if(comboCount == 2)
        {
            rightHandCollider.enabled = true;
            anim.SetInteger(hashComboCount, comboCount);
            yield return new WaitForSeconds(1f);
            rightHandCollider.enabled = false;
        }

        // 후딜레이를통해 의도치않은 코루틴실행막기
        yield return new WaitForSeconds(0.3f);               

        // 공격초기화
        comboCount = 0;
        anim.SetInteger(hashComboCount, comboCount);
        isComboAllowed = false;
        isAttack = false;
    }
}

  • 최초 공격키입력시에 BaseAttackPunch() 함수가 실행됩니다.
    • comboCount = 1 이되고, Attack 트리거를 발생시켜 애니메이션을 실행합니다.
    • 애니메이션에 맞게 콜라이더를 활성화/비활성화시킵니다.
    • 첫타가 나가는동안 일정시간 추가입력을 하게되면 comboCount를 증가시킵니다.
  • comboCount가 2가되고, 두번째 콤보공격이 나갑니다.
  • 후에 여유시간을 두어 애니메이션이 완전히 끝나기전 공격키입력이 되는것을 막습니다.
  • 완전히 애니메이션이 끝나면 공격과 관련된 로직을 초기화합니다.

댓글남기기