이번 포스트에서는 캐릭터의 스킬 중에서 단검을 투척하는 스킬을 구현해보겠습니다.
우선, 캐릭터가 단검을 투척하는 동작을 수행할 수 있도록 설정하는 과정과 구성을 알아보겠습니다.
1) 사용하는 요소
1) 단검 투척 스킬을 사용하기 위한 액션
2) 단검 조준 / 단검 투척 / 단검 회수의 3개의 애니메이션
3) 단검 투척시 Instantiate 함수로 생성할 단검 프리팹
4) 단검이 박히거나 / 단검이 움직이는 동안 연출될 단검의 애니메이션
2) 애니메이터의 구성
1) Player의 애니메이터
2) 단검(=Sword)의 애니메이터
위와 같이 필요한 구성을 준비하였습니다.
이제 코드로 넘어가보겠습니다.
3) 코드
3 - 1) PlayerStateAimSword
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStateAimSword : PlayerState
{
public PlayerStateAimSword(PlayerController inController, PlayerStateMachine inStateMachine, string inParamName)
: base(inController, inStateMachine, inParamName)
{
}
public override void Enter()
{
base.Enter();
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
if (_isThrowingSword == false)
_stateMachine.ChangeState(_controller._idleState);
}
}
마우스 우클릭을 통해 단검을 투척하기 위해 조준하는 상태인 AimState에서는
Update 메서드 안에서 우클릭 여부를 판단하여 false라면 다시 Idle 상태로 돌아가게 됩니다.
3 - 2) PlayerStateCatchSword
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerStateCatchSword : PlayerState
{
public PlayerStateCatchSword(PlayerController inController, PlayerStateMachine inStateMachine, string inParamName)
: base(inController, inStateMachine, inParamName)
{
}
public override void Enter()
{
base.Enter();
}
public override void Exit()
{
base.Exit();
}
public override void Update()
{
base.Update();
}
}
되돌아온 검을 다시 잡는 상태인 CatchState의 경우 정의만 하고 아직 사용은 하지 않습니다.
3 - 3) PlayerController
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using Unity.Burst;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : BaseCharacterController
{
// Animtion StateMachine
public PlayerStateMachine _stateMachine { get; private set; }
#region States
.
.
.
public PlayerStateCatchSword _catchSwordState { get; private set; }
#endregion
// Player Input
.
.
.
[SerializeField]
InputAction _throwSwordAction;
.
.
.
// Skill Info
public SkillManager _skillManager { get; private set; }
public bool _isThrowSwordClicked;
.
.
.
private void OnEnable()
{
.
.
.
_throwSwordAction.performed += DoThrowSword;
_throwSwordAction.canceled += DoStopThrowSword;
_throwSwordAction.Enable();
}
private void OnDisable()
{
.
.
.
_throwSwordAction.performed -= DoThrowSword;
_throwSwordAction.canceled -= DoStopThrowSword;
_throwSwordAction.Disable();
}
protected override void Awake()
{
.
.
.
_aimSwordState = new PlayerStateAimSword(this, _stateMachine, "AimSword");
_catchSwordState = new PlayerStateCatchSword(this, _stateMachine, "CatchSword");
}
.
.
.
void DoThrowSword(InputAction.CallbackContext value)
{
_isThrowSwordClicked = value.ReadValueAsButton();
}
void DoStopThrowSword(InputAction.CallbackContext value)
{
_isThrowSwordClicked = value.ReadValueAsButton();
}
.
.
.
}
Player를 의미하면서 동시에 플레이어의 조종을 담당하는 PlayerController에
새롭게 추가된 PlayerState인 AimState와 ThrowSwordState를 설정할 수 있도록 코드를 추가하고
동시에 단검을 투척하기 위한 Action을 추가하였습니다.
3 - 4) PlayerState
public class PlayerState
{
.
.
.
// Player Input
.
.
.
protected bool _isThrowingSword;
.
.
.
// State에서 매 프레임마다 진행
public virtual void Update()
{
.
.
.
_isThrowingSword = _controller._isThrowSwordClicked;
.
.
.
}
.
.
.
}
모든 PlayerState의 조상 클래스인 PlayerState에서는 PlayerController에서 수행한
단검 투척 액션의 입력 상태를 전달하기 위해 위와 같이 로직을 추가하였습니다.
3 - 5) PlayerStateGrounded
public class PlayerStateGrounded : PlayerState
{
.
.
.
public override void Update()
{
base.Update();
if (_isThrowingSword)
_stateMachine.ChangeState(_controller._aimSwordState);
.
.
.
}
}
단검 투척은 기본적으로 땅에 발을 딛고 있는 경우 수행하게 됩니다.
따라서 발을 땅에 딛고 있는 상태인 PlayerStateGrounded에서
_isThrowingSword를 판단하여 현재 플레이어의 상태를 AimSwordState로 변경하게 됩니다.
3 - 6) PlayerAnimationTrigger
public class PlayerAnimationTrigger : MonoBehaviour
{
.
.
.
private void ThrowSword()
{
SkillManager._skillManagerInstance._skillThrowingSword.CreateSword();
}
}
플레이어의 애니메이션 트리거에 사용할 함수들을 모아놓은 PlayerAnimationTrigger에서는
단검을 투척하는 애니메이션의 특정 프레임에서 단검을 생성하도록 위와 같은 함수를 추가하였습니다.
다음으로 단검을 위한 스크립트를 작업해보겠습니다.
'유니티 > 게임 프로젝트' 카테고리의 다른 글
2D RPG - (15 - 3) 단검 투척 스킬 구현_03 (0) | 2025.01.28 |
---|---|
2D RPG - (15 - 2) 단검 투척 스킬 구현_02 (0) | 2025.01.28 |
2D RPG - (14) 분신술 스킬 구현 (0) | 2025.01.07 |
2D RPG - (13) 스킬 시스템 구현을 위한 사전설정 (0) | 2025.01.06 |
2D RPG - (12 - 2) 카운터 어택 구현하기 (0) | 2024.12.31 |