캐릭터 기본 이동 기능 구현
This commit is contained in:
+511
-358
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 01631e903c99249258c1cf4651fc7cc9
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 80221b1715d134845ad8d6ecd262fe16
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4232fb211897c457ebc46ee353f640c0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,93 @@
|
||||
using UnityEngine;
|
||||
|
||||
public struct Inputs
|
||||
{
|
||||
public Vector2 PlayerMove;
|
||||
}
|
||||
|
||||
public class InputContoller
|
||||
{
|
||||
public static void UpdateInput(ref Inputs input)
|
||||
{
|
||||
input.PlayerMove.x = Input.GetAxisRaw("Horizontal");
|
||||
input.PlayerMove.y = Input.GetAxisRaw("Vertical");
|
||||
}
|
||||
}
|
||||
|
||||
public class PlayerMove : MonoBehaviour
|
||||
{
|
||||
// 컴포넌트 프로퍼티
|
||||
public Rigidbody2D rb { get; private set; }
|
||||
|
||||
private float maxSpeed;
|
||||
|
||||
private Inputs _inputs;
|
||||
public Inputs Inputse => _inputs;
|
||||
|
||||
// 이동 관련 변수
|
||||
[SerializeField] private float speed = 5f; // 기본 속도 설정
|
||||
[SerializeField] public int hp = 5;
|
||||
|
||||
// 캐릭터의 기본 바라보는 방향 상태를 저장 (왼쪽 = true, 오른쪽 = false)
|
||||
private bool isFacingRight = false;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// Rigidbody2D 컴포넌트 자동 할당
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
|
||||
// 캐릭터 방향 기본 오른쪽 설정 (기존 오타 수정: initailScale -> initialScale)
|
||||
Vector3 initialScale = transform.localScale;
|
||||
initialScale.x = Mathf.Abs(initialScale.x); // 확실하게 양수(오른쪽)로 초기화
|
||||
transform.localScale = initialScale;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
InputContoller.UpdateInput(ref _inputs);
|
||||
|
||||
// 입력값에 따른 방향 전환 체크
|
||||
if (_inputs.PlayerMove.x < 0f && isFacingRight)
|
||||
{
|
||||
// 왼쪽 키를 누르고 있고, 현재 오른쪽을 보고 있다면 왼쪽으로 반전
|
||||
Flip();
|
||||
}
|
||||
else if (_inputs.PlayerMove.x > 0f && !isFacingRight)
|
||||
{
|
||||
// 오른쪽 키를 누르고 있고, 현재 왼쪽을 보고 있다면 오른쪽으로 반전
|
||||
Flip();
|
||||
}
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
// 물리 연산 주기에 맞춰 실제로 캐릭터를 이동시킴
|
||||
Move();
|
||||
}
|
||||
|
||||
private void Move()
|
||||
{
|
||||
// rb가 비어있다면 이동 로직을 수행하지 않고 탈출
|
||||
if (rb == null)
|
||||
{
|
||||
Debug.LogError($"{gameObject.name} 오브젝트에 Rigidbody2D 컴포넌트가 없습니다!");
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ba0864ae82c04db9a0ab083a089a388
|
||||
Reference in New Issue
Block a user