2 분 소요

Intro

이번 포스팅에서는 무기별 공격애니메이션의 설정을 구현해보았습니다.

제가 구현할 방법은 이렇습니다.

  1. 무기를 장착/해제 할 때 플레이어의 특정위치(예를들어, 손)에 무기 프리팹 생성 및 파괴
    • 무기가 없을경우에는 주먹 프리팹 생성
    • 각 무기 프리팹에는 무기에 따른 Weapon 클래스(예를들어 Punch, Sword, Staff…)가 붙어있음.
  2. 현재 플레이어의 무기의 타입을 가져와 그에 따른 애니메이터의 “WeaponType” 파라미터의 값을 설정
  3. 공격키 입력시 “Attack” 트리거 발동 및 애니메이션 수행

정리해보면,

게임이 시작될 때 플레이어가 장착중인 무기타입을 가져와서 “WeaponType”을 설정.

공격키 입력을 통해 “Attack” 트리거 발동 및 “WeaponType” 에 맞는 애니메이션 수행

장비장착 및 해제시 “WeaponType” 변경 및 무기에 따른 공격전략 수행

WeaponType.cs

WeaponType 스크립트는 무기타입을 열거형으로 선언합니다.

/*
                WeaponType : 무기 타입별 인덱스
 */
public enum WeaponType
{
    None,   // 0: 맨손 
    Sword,  // 1: 검 
    Bow,    // 2: 활 
    Staff   // 3: 스태프
}

PlayerController.cs

PlayerController 클래스에서 공격키 입력을 받아 “Attack” 트리거를 발동시킵니다.

    [SerializeField] WeaponController weaponController;
    readonly private int hashAttackTrigger = Animator.StringToHash("Attack");

   private void GetInput()
    {
        isAttackKeyDown = Input.GetButtonDown("Attack");
    }
    // 플레이어 공격
    private void Attack()
    {
        if (isAttackKeyDown)
        {
            weaponController.Attack();
            anim.SetTrigger(hashAttackTrigger);
        }
    }

WeaponController.cs

WeaponController 클래스에서 현재 무기 설정 및 “WeaponType” 파라미터 값을 지정합니다.

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

/*
                    WeaponController : 무기 및 전략 관리

            - SetWeapon() : 현재 무기 설정
            - ChangeAttackStrategy() : 공격 전략 변경
            - Attack() : 현재 무기의 공격 실행
 */

public class WeaponController : MonoBehaviour
{
    [SerializeField] PlayerController player;
    [SerializeField] GameObject myWeapon;

    private Weapon currentWeapon;
    private WeaponType weaponType;

    readonly private int hashWeaponType = Animator.StringToHash("WeaponType");

    // 무기 타입별 AttackType 파라미터값 
    public int AttackType
    {
        get => player.Anim.GetInteger(hashWeaponType);
        set => player.Anim.SetInteger(hashWeaponType, value);
    }

    private void Start()
    {
        SetWeapon();
    }

    // 현재 무기 세팅
    public void SetWeapon()
    {
        currentWeapon = myWeapon.GetComponentInChildren<Weapon>();          // 현재 장착중인 무기 가져오기
        
        // 무기가 없을 때(임시)
        if(currentWeapon == null)
        {
            weaponType = WeaponType.None;
        }
        else
        {
            weaponType = currentWeapon.type;
        }

        // 무기별 애니메이션
        AttackType = (int)weaponType;
    }

    // 공격 전략 변경
    public void ChangeAttackStrategy(IAttackStrategy newStrategy)
    {
        if(currentWeapon != null)
        {
            currentWeapon.SetAttackStrategy(newStrategy);
        }
        // 무기가 없을 때(임시)
        else
        {
            Debug.Log("무기가 장착되지 않았음");
        }
    }

    // 현재 무기 공격 실행
    public void Attack()
    {
        if(currentWeapon != null)
        {
            currentWeapon.PerformAttack();
        }
        // 무기가 없을 때(임시)
        else
        {
            Debug.Log("무기가 장착되지 않았음");
        }
    }
}
  • myWeapon은 무기 프리팹을 생성할 부모오브젝트입니다.
    • 무기를 쥐고있을 손의 위치에 있습니다.
    • 자식 오브젝트에 무기가 생성될것이며, 그 정보를 가져옵니다.
  • 무기를 생성하는 부분은 아직 구현하지 않았습니다.

PunchAttack.cs

PunchAttack 클래스는 무기가 없을때의 주먹 공격 전략입니다.

public class PunchAttack : IAttackStrategy
{
    // 공격
    public void Attack()
    {
        Debug.Log("주먹 공격 수행");
    }
}

Punch.cs

Punch 클래스는 주먹 무기클래스입니다.

/*
                    Punch : 무기(주먹) 클래스
 */
public class Punch : Weapon
{
    private void Awake()
    {
        // 무기 타입 설정
        type = WeaponType.None;
    }
    private void Start()
    {
        // 기본 공격 전략 설정
        attackStrategy = new PunchAttack();
    }
}
  • 무기 타입을 설정합니다.

테스트

무기가 장착되지 않았을때와 무기를 장착했을때 애니메이션이 다르게 실행되는지 확인해봤습니다.

image

image

댓글남기기