오버플로우로 유니티 먹통되는 코드 수정

This commit is contained in:
Mingu Kim
2025-08-15 20:53:23 +09:00
parent 89a2bfa424
commit f2a72a8aa2
2 changed files with 19 additions and 18 deletions

View File

@@ -31,6 +31,15 @@ public class PlayerMove : MonoBehaviour
RigidBody = GetComponent<Rigidbody2D>();
SpriteRenderer = GetComponent<SpriteRenderer>();
Animator = GetComponent<Animator>();
// 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()로 이동
}
}
}

View File

@@ -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;