마우스 클릭시 스크린 좌표를 월드 좌표로 변환해 출력
메인카메라에 할당된 카메라 기준
메서드 사용하기 전 스크린 좌표가 현재 카메라의 뷰포트 내에 있는지 확인해야함
2D의 경우 orthograhic을 true로 설정 필요
if (Input.GetMouseButtonDown(0))
Debug.Log($"mousePosition = {Input.mousePosition}");
Vector3 pos = Input.mousePosition;
pos.z = 10.0f;
Vector3 wpos = Camera.main.ScreenToWorldPoint(pos);
vieport
카메라가 보는 영역을 나타내는 상대적인 좌표 시스템
0~1의 범위를 가진 정규화된 값 표현
특정 위치가 카메라 시야 내에 있는지 판단 할 때
스크린 좌표를 월드 좌표로 변환할 떄 활용
뷰포트 좌표는 카메라 시야의 상대적인 값으로 표현헤 화면 크기에 관계없이 일관된 결과를 보여줌
WorldToViewportPoint
뷰포트 좌표와 월드 좌표 간의 변환을 수행
aspect Ratio
화면의 가로와 세로 비율 ex>16:9
unity에서 camera 컴포넌트 aspect속성,screen 클래스로 확인 가능
ScreenPointToRay
스크린 좌표를 기준으로 광선을 생성
ray를 사용해 충돌 감지 등의 rayCast작업 수행
rayCast : collision detection/ object picking(식별), hit dectection
RaycastAll
ScreenPointToRay와 함께 사용
스크린 좌표 기준으로 광선 생성 , 광선에 충돌하는 모든 객체를 찾음
배열을 이용해 충돌한 객체를 담음
RaycastHit[] hits;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
hits = Physics.RaycastAll(ray);
for (int i = 0; i < hits.Length; i++)
{
Debug.Log(hits[i].collider.gameObject.name);
if (hits[i].collider.gameObject.name == "오브젝트 이름")
{
Destroy(hits[i].collider.gameObject);
}
}