Files
XRLib/Assets/Scripts/SampleProject/RaycastDebugger.cs
2025-06-24 14:38:58 +09:00

42 lines
1.3 KiB
C#

using UnityEngine;
public class RaycastDebugger : MonoBehaviour
{
private Camera mainCamera;
void Start()
{
// Camera.main 대신 시작할 때 한 번만 카메라를 찾아오는 것이 더 안전하고 효율적입니다.
mainCamera = Camera.main;
if (mainCamera == null)
{
Debug.LogError("씬에 'MainCamera' 태그가 붙은 카메라가 없습니다!");
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (mainCamera == null) return;
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Scene 뷰에서 Ray를 시각적으로 확인하기 위한 디버그용 코드
Debug.DrawRay(ray.origin, ray.direction * 100f, Color.yellow, 120f);
// Ray 객체를 직접 넘기는 것이 더 간단합니다.
// 거리는 무한대(Mathf.Infinity)로 설정합니다.
if (Physics.Raycast(ray, out hit))
{
Debug.Log("Raycast Hit! 맞은 객체: " + hit.collider.name);
// 여기에 원하는 로직을 추가하세요.
}
else
{
Debug.Log("Raycast Missed. 아무것도 맞지 않았습니다.");
}
}
}
}