공격 스테이트 추가 + 플레이어가 우측으로 이동 하다 멈추면 왼쪽을 바라보는 문제 수정

This commit is contained in:
Mingu Kim
2025-08-19 20:56:07 +09:00
parent 9ecbcc3396
commit d8c356e841
3 changed files with 136 additions and 13 deletions

View File

@@ -17,6 +17,9 @@ public class PlayerMove : MonoBehaviour
private StateMachine _stateMachine;
// 공격 콜라이더
public Collider2D attackCollider;
// 사용하는 컴포넌트들 초기화
void Awake()
{
@@ -35,6 +38,9 @@ public class PlayerMove : MonoBehaviour
}
_stateMachine = new StateMachine(State.IDLE, this);
// 공격 콜라이더 참조
attackCollider = GetComponentInChildren<Collider2D>();
}
private void Update()
@@ -76,4 +82,21 @@ public class PlayerMove : MonoBehaviour
{
// OffDamaged 로직은 DamagedState의 Exit()로 이동
}
// 공격 애니메이션 이벤트에서 호출될 함수
public void StartAttackHitbox()
{
if (attackCollider != null)
{
attackCollider.enabled = true;
}
}
public void EndAttackHitbox()
{
if (attackCollider != null)
{
attackCollider.enabled = false;
}
}
}

View File

@@ -3,7 +3,7 @@ using UnityEngine;
public enum State
{
IDLE, MOVE, JUMP
IDLE, MOVE, JUMP, Attack
}
public class StateMachine
@@ -116,8 +116,12 @@ public class MoveState : IState
public void Update()
{
// 상태별 로직
_player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") > 0;
float horizontalInput = Input.GetAxisRaw("Horizontal");
if (horizontalInput != 0)
{
// 입력 방향에 따라 스프라이트를 뒤집음.
_player.SpriteRenderer.flipX = horizontalInput > 0;
}
}
public void Exit()
@@ -189,8 +193,7 @@ public class JumpState : IState
public class AttackState : IState
{
private PlayerMove _player;
private IdleState _idleState;
public AttackState(PlayerMove player)
{
_player = player;
@@ -198,28 +201,35 @@ public class AttackState : IState
public void Enter()
{
// 공격 애니메이션 시작
_player.Animator.SetTrigger("Attack");
}
public void Update()
{
// 공격 상태 로직 (예: 애니메이션 재생 중 다른 입력 무시)
}
public void FixedUpdate()
{
}
public void Exit()
{
// _player.Animator.SetBool(PlayerMove.IsAttacking, false);
// 공격 상태 종료
}
public State CheckTransition()
{
// 공격이 종료됐을 때
// idle
return State.IDLE;
// 공격 애니메이션이 끝나면 Idle 상태로 전환
// Animator의 현재 상태를 확인하여 transition을 처리합니다.
// 예를 들어, GetCurrentAnimatorStateInfo(0).IsName("Attack")이 false가 되면 전환
// 또는 애니메이션 이벤트로 상태 전환을 직접 호출할 수도 있습니다.
AnimatorStateInfo stateInfo = _player.Animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.normalizedTime >= 1.0f && !stateInfo.IsTag("Attack"))
{
return State.IDLE;
}
return State.Attack;
}
}