From f2a72a8aa234ba253b3432106a20c5be0f817469 Mon Sep 17 00:00:00 2001 From: Mingu Kim Date: Fri, 15 Aug 2025 20:53:23 +0900 Subject: [PATCH] =?UTF-8?q?=EC=98=A4=EB=B2=84=ED=94=8C=EB=A1=9C=EC=9A=B0?= =?UTF-8?q?=EB=A1=9C=20=EC=9C=A0=EB=8B=88=ED=8B=B0=20=EB=A8=B9=ED=86=B5?= =?UTF-8?q?=EB=90=98=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Player/PlayerMove.cs | 11 ++++++++++- Assets/Scripts/Player/StateMachine.cs | 26 +++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/Player/PlayerMove.cs b/Assets/Scripts/Player/PlayerMove.cs index 2a9bbb11..d6682791 100644 --- a/Assets/Scripts/Player/PlayerMove.cs +++ b/Assets/Scripts/Player/PlayerMove.cs @@ -31,6 +31,15 @@ public class PlayerMove : MonoBehaviour RigidBody = GetComponent(); SpriteRenderer = GetComponent(); Animator = GetComponent(); + + // Null 체크 추가 + if (RigidBody == null || SpriteRenderer == null || Animator == null) + { + Debug.LogError("PlayerMove script requires Rigidbody2D, SpriteRenderer, and Animator components on the same GameObject."); + // 게임 오브젝트 비활성화 또는 스크립트 비활성화 + enabled = false; + return; + } // 2. Animator가 초기화된 후에 해시 값 할당 _isJumping = Animator.StringToHash("isJumping"); @@ -68,4 +77,4 @@ public class PlayerMove : MonoBehaviour { // OffDamaged 로직은 DamagedState의 Exit()로 이동 } -} \ No newline at end of file +} diff --git a/Assets/Scripts/Player/StateMachine.cs b/Assets/Scripts/Player/StateMachine.cs index 3792256d..a82f4f76 100644 --- a/Assets/Scripts/Player/StateMachine.cs +++ b/Assets/Scripts/Player/StateMachine.cs @@ -59,8 +59,6 @@ public class IdleState : IState public IdleState(PlayerMove player) { _player = player; - _moveState = new MoveState(_player); - _jumpState = new JumpState(_player); } public void Enter() @@ -85,14 +83,13 @@ public class IdleState : IState public IState CheckTransition() { - // 우선 순위: Jump -> Move if (Input.GetButtonDown("Jump")) { - return _jumpState; + return new JumpState(_player); // Create new JumpState object } - if (Input.GetAxisRaw("Horizontal") != 0) + if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0) { - return _moveState; + return new MoveState(_player); // Create new MoveState object } return this; } @@ -107,8 +104,6 @@ public class MoveState : IState public MoveState(PlayerMove player) { _player = player; - _idleState = new IdleState(_player); - _jumpState = new JumpState(_player); } public void Enter() @@ -119,7 +114,7 @@ public class MoveState : IState public void Update() { // 상태별 로직 - _player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") < 0; + _player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") > 0; } public void FixedUpdate() @@ -147,11 +142,11 @@ public class MoveState : IState { if (Input.GetButtonDown("Jump")) { - return _jumpState; + return new JumpState(_player); // Create new JumpState object } - if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.1f) // 키 입력이 없을 때 + if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.1f) { - return _idleState; + return new IdleState(_player); // Create new IdleState object } return this; } @@ -165,7 +160,6 @@ public class JumpState : IState public JumpState(PlayerMove player) { _player = player; - _idleState = new IdleState(_player); } public void Enter() @@ -179,7 +173,7 @@ public class JumpState : IState // 점프 중 스프라이트 방향 변경 if (Input.GetButton("Horizontal")) { - _player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") < 0; + _player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") > 0; } } @@ -195,15 +189,13 @@ public class JumpState : IState public IState CheckTransition() { - // 바닥에 닿았는지 확인 -> IdleState로 전환 if (_player.RigidBody.linearVelocity.y < 0) { - Debug.DrawRay(_player.RigidBody.position, Vector3.down, Color.green); RaycastHit2D rayHit = Physics2D.Raycast(_player.RigidBody.position, Vector3.down, 1, LayerMask.GetMask("Platform")); if (rayHit.collider != null && rayHit.distance < 0.7f) { - return _idleState; + return new IdleState(_player); // Create new IdleState object } } return this;