캐릭터 이동 및 애니메이션(이동, 공격) 구현
This commit is contained in:
@@ -19,73 +19,88 @@ public class PlayerMove : MonoBehaviour
|
||||
// 컴포넌트 프로퍼티
|
||||
public Rigidbody2D rb { get; private set; }
|
||||
|
||||
private float maxSpeed;
|
||||
// [수정] 동일한 오브젝트에 있는 SPUM_Prefabs를 가져옵니다.
|
||||
private SPUM_Prefabs spumPrefab;
|
||||
|
||||
private float maxSpeed;
|
||||
private Inputs _inputs;
|
||||
public Inputs Inputse => _inputs;
|
||||
|
||||
// 이동 관련 변수
|
||||
[SerializeField] private float speed = 5f; // 기본 속도 설정
|
||||
[SerializeField] private float speed = 5f;
|
||||
[SerializeField] public int hp = 5;
|
||||
|
||||
// 캐릭터의 기본 바라보는 방향 상태를 저장 (왼쪽 = true, 오른쪽 = false)
|
||||
private bool isFacingRight = false;
|
||||
private int currentAnimationState = -1;
|
||||
|
||||
private Animator animator;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// Rigidbody2D 컴포넌트 자동 할당
|
||||
// 1. Rigidbody2D 할당
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
|
||||
// 캐릭터 방향 기본 오른쪽 설정 (기존 오타 수정: initailScale -> initialScale)
|
||||
// 2. [수정] 같은 오브젝트에 붙어있으므로 GetComponent로 정확히 가져옵니다.
|
||||
spumPrefab = GetComponentInParent<SPUM_Prefabs>();
|
||||
// spumPrefab = GetComponent<SPUM_Prefabs>();
|
||||
|
||||
if (spumPrefab == null)
|
||||
{
|
||||
Debug.LogError($"{gameObject.name} 오브젝트에 SPUM_Prefabs 컴포넌트가 없습니다! 인스펙터를 확인하세요.");
|
||||
}
|
||||
|
||||
// 캐릭터 방향 기본 오른쪽 설정
|
||||
Vector3 initialScale = transform.localScale;
|
||||
initialScale.x = Mathf.Abs(initialScale.x); // 확실하게 양수(오른쪽)로 초기화
|
||||
initialScale.x = Mathf.Abs(initialScale.x);
|
||||
transform.localScale = initialScale;
|
||||
|
||||
animator = GetComponentInChildren<Animator>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
InputContoller.UpdateInput(ref _inputs);
|
||||
|
||||
// 입력값에 따른 방향 전환 체크
|
||||
// 좌우 방향 전환 (Flip)
|
||||
if (_inputs.PlayerMove.x < 0f && isFacingRight)
|
||||
{
|
||||
// 왼쪽 키를 누르고 있고, 현재 오른쪽을 보고 있다면 왼쪽으로 반전
|
||||
Flip();
|
||||
}
|
||||
else if (_inputs.PlayerMove.x > 0f && !isFacingRight)
|
||||
{
|
||||
// 오른쪽 키를 누르고 있고, 현재 왼쪽을 보고 있다면 오른쪽으로 반전
|
||||
Flip();
|
||||
}
|
||||
|
||||
// 애니메이션 업데이트 호출
|
||||
if (_inputs.PlayerMove.x != 0f || _inputs.PlayerMove.y != 0f)
|
||||
{
|
||||
animator.SetBool("1_Move", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
animator.SetBool("1_Move", false);
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Z))
|
||||
{
|
||||
animator.SetTrigger("2_Attack");
|
||||
}
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
// 물리 연산 주기에 맞춰 실제로 캐릭터를 이동시킴
|
||||
Move();
|
||||
}
|
||||
|
||||
private void Move()
|
||||
{
|
||||
// rb가 비어있다면 이동 로직을 수행하지 않고 탈출
|
||||
if (rb == null)
|
||||
{
|
||||
Debug.LogError($"{gameObject.name} 오브젝트에 Rigidbody2D 컴포넌트가 없습니다!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (rb == null) return;
|
||||
rb.linearVelocity = new Vector2(_inputs.PlayerMove.x * speed, rb.linearVelocity.y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 캐릭터의 좌우 스케일을 반전시키는 메서드
|
||||
/// </summary>
|
||||
private void Flip()
|
||||
{
|
||||
// 상태 스위칭
|
||||
isFacingRight = !isFacingRight;
|
||||
|
||||
// localScale의 x축에 -1을 곱하여 반전
|
||||
Vector3 scale = transform.localScale;
|
||||
scale.x *= -1f;
|
||||
transform.localScale = scale;
|
||||
|
||||
Reference in New Issue
Block a user