# Raycasting 종류
1. Physics.Raycast: 하나만 감지
2. Physics.RaycastAll: 모두 감지
# Raycasting 사용 예시
- Raycast 버전
Vector3 look = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position + Vector3.up, look * 10, Color.red);
RaycastHit hit;
if (Physics.Raycast(transform.position + Vector3.up, look, out hit, 10))
{
Debug.Log($"Raycast {hit.collider.gameObject.name}");
}
- RaycastAll 버전
Vector3 look = transform.forward;
Debug.DrawRay(transform.position + Vector3.up, look * 10, Color.red);
RaycastHit[] hits;
hits = Physics.RaycastAll(transform.position + Vector3.up, look, 10);
foreach(RaycastHit hit in hits)
{
Debug.Log($"Raycast {hit.collider.gameObject.name}");
}
# transform.TransformDireciton/Point
특정 객체의 로컬좌표 방향을 받아서 레이캐스팅을 하고 싶은 경우에는
로컬에서 월드좌표계로 변환해야 한다
transfrom.TransformDirection(): 로컬에서 월드 좌표계로 방향을 변환
transfrom.TransformPoint(): 로컬에서 월드 좌표계로 좌표를 변환
Vector3 look = Vector3.forward;
위의 코드는 월드좌표계에서 forward이기에
Vector3 look = transform.TransformDirection(Vector3.forward);
위의 코드처럼 로컬좌표계에서 forward를 가져와야 한다는 것이다
transfrom.TransformDirection()은 다소 번거롭기 때문에
transform.forward, transform.up, transform.right 이렇게 작성을 해도
로컬 좌표계 기준 방향을 쉽게 가져올 수 있다
Vector3 look = transform.forward;
'게임 엔진 - 유니티 > [루키스] 유니티' 카테고리의 다른 글
| [유니티 Note] 섹션 5-6 (0) | 2025.07.02 |
|---|---|
| [유니티 Note] 섹션 5-5 (0) | 2025.07.02 |
| [유니티 Note] 섹션 5-3 (0) | 2025.07.02 |
| [유니티 Note] 섹션 5-2 (0) | 2025.07.02 |
| [유니티 Note] 섹션 5-1 (0) | 2025.07.02 |