프로젝트 작업중 Character Controller 의 속성인 isGrounded가 제대로 동작하지 않음을 확인하였다.
검색하여 알아낸 하나의 방법은 Character Controller의 Min Move Distance를 0으로 수정하는 것이였다. 전보다는 훨씬 좋은 판정을 가지게 됐지만, 울퉁불퉁한 내리막에서는 여전히 제대로 동작하지 않았다.
다른 방법은 Ray를 바닥에 쏴서 충돌 판정을 하는 것 이었는데 이 방법으로 해결하였다.
Code
if (IsGroundedUsingRay() && Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
anim.SetTrigger("Jump");
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
private bool IsGroundedUsingRay()
{
// CharacterController.IsGrounded가 true라면 Raycast를 사용하지 않고 판정 종료
if (controller.isGrounded) return true;
// 발사하는 광선의 초기 위치와 방향
var ray = new Ray(this.transform.position + Vector3.up * 0.1f, Vector3.down);
// 탐색 거리
var maxDistance = 1.5f;
// 광선 디버그 용도
Debug.DrawRay(transform.position + Vector3.up * 0.1f, Vector3.down * maxDistance, Color.red);
// Raycast의 hit 여부로 판정
return Physics.Raycast(ray, maxDistance, _fieldLayer);
}
Terrain Layer에 Ground를 추가해주었고 Player Script에 Field Layer를 Ground로 설정하니 정상적으로 동작했다.