몬스터의 ground check는 다음과 같은 이유를 비롯하여 필요로 한다.
- 땅 위에 있는지(낙하/점프/계단) 판단
- 경사/단차/절벽 감지(앞으로 가도 되는지)
- 네비/이동 로직 보정(공중에서 이동 금지, 슬립 방지, 스텝 처리)
따라서 오늘은 스켈레톤 클래스에서 ground check를 가능하게 하도록
상위 구조의 코드를 변경하였다.
1. 부모 클래스의 코드 수정
아래는 변경 전 코드이다.
private void HandleCollisionDetection()
{
groundDetected = Physics2D.Raycast(transform.position, Vector2.down, groundCheckDistance, whatIsGround);
wallDetected = Physics2D.Raycast(primaryWallCheck.position, Vector2.right * facingDir, wallCheckDistance, whatIsGround)
&& Physics2D.Raycast(secondaryWallCheck.position, Vector2.right * facingDir, wallCheckDistance, whatIsGround);
}
private void OnDrawGizmos()
{
Gizmos.DrawLine(transform.position, transform.position + new Vector3(0, -groundCheckDistance));
Gizmos.DrawLine(primaryWallCheck.position, primaryWallCheck.position + new Vector3(wallCheckDistance * facingDir, 0));
Gizmos.DrawLine(secondaryWallCheck.position, secondaryWallCheck.position + new Vector3(wallCheckDistance * facingDir, 0));
}
아래는 변경 후의 코드이다.
private void HandleCollisionDetection() {
groundDetected = Physics2D.Raycast
(groundCheck.position, Vector2.down, groundCheckDistance, whatIsGround);
wallDetected = Physics2D.Raycast(
primaryWallCheck.position, Vector2.right * facingDir, wallCheckDistance, whatIsGround) &&
Physics2D.Raycast(secondaryWallCheck.position, Vector2.right *
facingDir, wallCheckDistance, whatIsGround);
}
private void OnDrawGizmos() {
Gizmos.DrawLine(groundCheck.position, groundCheck.position +
new Vector3(0, -groundCheckDistance));
Gizmos.DrawLine(primaryWallCheck.position,
primaryWallCheck.position + new Vector3(wallCheckDistance * facingDir, 0));
Gizmos.DrawLine(secondaryWallCheck.position, secondaryWallCheck.position + new Vector3(wallCheckDistance * facingDir, 0));
}
두 코드의 차이점은 기준 위치가 캐릭터 중심 위치인가?
groundCheck Transform인 (주로 발 밑)의 위치 기준인가?
의 차이가 있다.
2. tranform 기준이 될 오브젝트 추가

해당 빈 오브젝트를 추가하여 ground를 체크할 수 있는 transform을 만들어 준다.

해당 컴포넌트를 대입시켜준다.
3. State 상태 별 코드 수정
// idle 상태
public override void Enter()
{
base.Enter();
stateTimer = enemy.idleTime;
}
public override void Update()
{
base.Update();
if (stateTimer < 0)
{
stateMachine.ChangeState(enemy.moveState);
}
}
// move 상태
public override void Update()
{
base.Update();
enemy.SetVelocity(enemy.moveSpeed * enemy.facingDir, rb.linearVelocity.y);
if(!enemy.groundDetected || enemy.wallDetected)
{
stateMachine.ChangeState(enemy.idleState);
enemy.Flip();
}
}
'DevLog > 유니티 프로젝트' 카테고리의 다른 글
| 2DRPG 타겟 탐지 기능 - ComBatSystem (1) | 2026.01.14 |
|---|---|
| 2D RPG - 플레이어를 인식하는 enemy AI (0) | 2026.01.13 |
| 유니티 구글 스프레드 시트 연동 툴 (4) | 2025.06.29 |
| DT 기능 에디터 만들기 GoogleSheetURLParser Class 설명 (2) | 2025.06.08 |
| 구글 엑셀 연동 DT 관리 - GoogleDTTemplate 클래스 정리 (1) | 2025.05.27 |