플랫폼별 입력 화장성을 위해 Inputs구조체 추가 및 InputControoller 추가

- 캐릭터 이동이 정상적으로 작동하지 않는 문제 수정
This commit is contained in:
Mingu Kim
2025-08-30 21:06:16 +09:00
parent 6eb081104f
commit 5b48bad730
3 changed files with 67 additions and 27 deletions

View File

@@ -1,5 +1,15 @@
using UnityEngine;
// public class AIInputController
// {
// public override void UpdateInput(ref Inputs inputs)
// {
// // Monsters 배열 정보 받아
// // 주변 환경 정보 받아오기
// // 환경 정보에 따라서 inputs 변경
// }
// }
public class BossEnemyMove : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
@@ -36,7 +46,8 @@ public class BossEnemyMove : MonoBehaviour
{
_rigidbody = GetComponent<Rigidbody2D>();
Invoke("Think", 5f); // 5초 후에 다시 생각 시작
// GC Alloc 최소화
Invoke(nameof(Think), 5f); // 5초 후에 다시 생각 시작
}
private void FixedUpdate()

View File

@@ -1,5 +1,24 @@
using System;
using UnityEngine;
using UnityEngine.PlayerLoop;
public struct Inputs
{
public bool Jump;
public Vector2 Move;
public bool Attack;
}
public class InputController
{
public static void UpdateInput(ref Inputs inputs)
{
inputs.Jump = Input.GetButtonDown("Jump");
inputs.Move.x = Input.GetAxisRaw("Horizontal");
inputs.Attack = Input.GetMouseButtonDown(0);
}
}
public class PlayerMove : MonoBehaviour
{
// 컴포넌트 프로퍼티
@@ -10,6 +29,10 @@ public class PlayerMove : MonoBehaviour
public float maxSpeed;
public float jumpPower;
private Inputs _inputs;
public Inputs Inputs => _inputs;
// 변수
[SerializeField]
public int hp = 5;
@@ -30,7 +53,7 @@ public class PlayerMove : MonoBehaviour
// Null 체크 추가
if (RigidBody == null || SpriteRenderer == null || Animator == null)
{
Debug.LogError("PlayerMove script requires Rigidbody2D, SpriteRenderer, and Animator components on the same GameObject.");
Debug.LogError("PlayerMove 스크립트에는 동일한 GameObject에 Rigidbody2D, SpriteRenderer Animator 구성 요소가 필요합니다.");
// 게임 오브젝트 비활성화 또는 스크립트 비활성화
enabled = false;
return;
@@ -52,18 +75,20 @@ public class PlayerMove : MonoBehaviour
if (attackCollider == null)
{
Debug.LogError("AttackArea child object or Collider2D component not found!");
Debug.LogError("AttackArea 자식 객체나 Collider2D 구성 요소를 찾을 수 없습니다!");
}
}
void Start()
{
}
private void Update()
{
// 모든 입력값을 매 프레임 업데이트
InputController.UpdateInput(ref _inputs);
// 상태 머신 업데이트
_stateMachine.Update();
// 프레임이 끝난 후, 다음 프레임을 위해 "GetButtonDown"으로 받는 입력들은 리셋( Jump와 Attack )
_inputs.Jump = false;
_inputs.Attack = false;
}
void OnCollisionEnter2D(Collision2D collision)

View File

@@ -67,6 +67,8 @@ public interface IState
public class IdleState : IState
{
private static readonly int IsMoving = Animator.StringToHash("isMoving");
private static readonly int IsJumping = Animator.StringToHash("isJumping");
private PlayerMove _player;
public IdleState(PlayerMove player)
@@ -76,8 +78,8 @@ public class IdleState : IState
public void Enter()
{
_player.Animator.SetBool("isMoving", false);
_player.Animator.SetBool("isJumping", false);
_player.Animator.SetBool(IsMoving, false);
_player.Animator.SetBool(IsJumping, false);
}
public void Update()
@@ -90,15 +92,16 @@ public class IdleState : IState
public State CheckTransition()
{
if (Input.GetButtonDown("Jump"))
if (_player.Inputs.Jump)
{
return State.JUMP;
}
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0)
// 수정: 좌우 어느 방향으로든 입력이 있을 때 MOVE 상태로 전환
if (_player.Inputs.Move != Vector2.zero)
{
return State.MOVE;
}
if (Input.GetMouseButtonDown(0))
if (_player.Inputs.Attack)
{
return State.Attack;
}
@@ -108,6 +111,7 @@ public class IdleState : IState
public class MoveState : IState
{
private static readonly int IsMoving = Animator.StringToHash("isMoving");
private PlayerMove _player;
public MoveState(PlayerMove player)
@@ -117,16 +121,14 @@ public class MoveState : IState
public void Enter()
{
_player.Animator.SetBool("isMoving", true);
_player.Animator.SetBool(IsMoving, true);
}
public void Update()
{
float horizontalInput = Input.GetAxisRaw("Horizontal");
float horizontalInput = _player.Inputs.Move.x;
if (horizontalInput != 0)
{
// transform.localScale.x를 사용하여 캐릭터와 자식 오브젝트를 함께 뒤집습니다.
// 기본 스프라이트가 왼쪽을 바라보고 있으므로, 오른쪽 이동 시 x 스케일을 뒤집어야 합니다.
Vector3 newScale = _player.transform.localScale;
newScale.x = horizontalInput > 0 ? -1f : 1f;
_player.transform.localScale = newScale;
@@ -142,20 +144,20 @@ public class MoveState : IState
public void Exit()
{
_player.Animator.SetBool("isMoving", false);
_player.Animator.SetBool(IsMoving, false);
}
public State CheckTransition()
{
if (Input.GetButtonDown("Jump"))
if (_player.Inputs.Jump)
{
return State.JUMP;
}
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.1f)
if (_player.Inputs.Move == Vector2.zero)
{
return State.IDLE;
}
if (Input.GetMouseButtonDown(0))
if (_player.Inputs.Attack)
{
return State.Attack;
}
@@ -165,6 +167,7 @@ public class MoveState : IState
public class JumpState : IState
{
private static readonly int IsJumping = Animator.StringToHash("isJumping");
private PlayerMove _player;
public JumpState(PlayerMove player)
@@ -175,27 +178,28 @@ public class JumpState : IState
public void Enter()
{
_player.RigidBody.AddForce(Vector2.up * _player.jumpPower, ForceMode2D.Impulse);
_player.Animator.SetBool("isJumping", true);
_player.Animator.SetBool(IsJumping, true);
}
public void Update()
{
if (Input.GetButton("Horizontal"))
float horizontalInput = _player.Inputs.Move.x;
if (horizontalInput != 0)
{
Vector3 newScale = _player.transform.localScale;
newScale.x = Input.GetAxisRaw("Horizontal") > 0 ? -1f : 1f;
newScale.x = horizontalInput > 0 ? -1f : 1f;
_player.transform.localScale = newScale;
}
}
public void Exit()
{
_player.Animator.SetBool("isJumping", false);
_player.Animator.SetBool(IsJumping, false);
}
public State CheckTransition()
{
if (Input.GetMouseButtonDown(0))
if (_player.Inputs.Attack)
{
return State.Attack;
}