Camera.ScreenPointToRay


카메라로 부터의 스크린의 점을 통해 레이를 반환합니다.

스크린공간은 픽셀로 정의되며.

픽셀단위

1번 : 왼쪽 하단의 화면이 (0,0)

2번: 오른쪽 상단이 (pixelWidth,pixelHeight)


if (Input.GetButtonDown("0"))

{


Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hit;

if (Physics.Raycast(ray, out hit, Mathf.Infinity))

{

print("raycast hit!");

Debug.DrawRay(ray.origin, ray.direction * 20, Color.red, 5f);

Debug.Log(hit.point);

}

}




Debug.Ray를 통해 어느부분을 클릭했는지 씬뷰에서 확인이 가능하다.

hit.point를 통해 스크린상 좌표값을 얻어 올 수 있다.

반응형

+ Recent posts