🕹️ 실습 (2D 횡스크롤 스테이트머신)
벽 슬라이드, 벽 점프 구현
public class PlayerWallSlideState : PlayerState
{
public PlayerWallSlideState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName)
: base(_player, _stateMachine, _animBoolName)
{
}
public override void Enter()
{
base.Enter();
}
public override void Update()
{
base.Update();
if (Input.GetKeyDown(KeyCode.Space))
{
stateMachine.ChangeState(player.wallJumpState);
return;
}
if (xInput != 0 && player.facingDir != xInput)
{
stateMachine.ChangeState(player.idleState);
}
if (yInput < 0)
{
player.SetVelocity(0, rb.linearVelocityY);
}
else
{
player.SetVelocity(0, rb.linearVelocityY * 0.7f);
}
if (player.IsGroundDetected())
{
stateMachine.ChangeState(player.idleState);
}
}
public override void Exit()
{
base.Exit();
}
}
public class PlayerWallJumpState : PlayerState
{
public PlayerWallJumpState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName)
: base(_player, _stateMachine, _animBoolName)
{
}
public override void Enter()
{
base.Enter();
stateTimer = 0.4f;
player.SetVelocity(5 * -player.facingDir, player.jumpForce);
}
public override void Update()
{
base.Update();
if (stateTimer < 0)
{
stateMachine.ChangeState(player.airState);
}
if (player.IsGroundDetected())
{
stateMachine.ChangeState(player.idleState);
}
}
public override void Exit()
{
base.Exit();
}
}
콤보 공격 구현
[Header("Attack Detail")]
public Vector2[] attackMovement;
public class PlayerPrimaryAttackState : PlayerState
{
private int comboCounter;
private float lastTimeAttacked;
private float comboWindow = 2;
public PlayerPrimaryAttackState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName)
: base(_player, _stateMachine, _animBoolName)
{
}
public override void Enter()
{
base.Enter();
if (comboCounter > 2 || Time.time >= lastTimeAttacked + comboWindow)
{
comboCounter = 0;
}
player.anim.SetInteger("ComboCounter", comboCounter);
float attackDir = xInput != 0 ? xInput : player.facingDir;
player.SetVelocity(player.attackMovement[comboCounter].x * attackDir, player.attackMovement[comboCounter].y);
stateTimer = 0.1f;
}
public override void Update()
{
base.Update();
if (stateTimer < 0)
{
player.SetZeroVelocity();
}
if (triggerCalled)
{
stateMachine.ChangeState(player.idleState);
}
}
public override void Exit()
{
base.Exit();
player.StartCoroutine("BusyFor", 0.1f);
comboCounter++;
lastTimeAttacked = Time.time;
}
}
💫 Tip!
이미지 피봇 변경