좌표계 종류
World Point
: 실제 gameObject의 transform.position 값
Viewport Point
: 카메라 내에서 오브젝트가 위치하고 있는 비율
따라서 값이 0~1 사이의 값으로 나타난다.
Viewpoint Point를 활용하면, 화면에 표시되지 않는 객체가 어디에 위치하는지를 알 수 있다.
이를 쉽게 이해하기 위해 아래와 같은 예시를 들어보겠다.
캐릭터는 맵의 한 쪽 끝에 있고, 그 맵의 다른 쪽에는 특정 아이템이 있다.
하지만 그 아이템은 현재 화면에 보이지 않는다.
이 때, Viewport Point를 활용하면 그 아이템이 어디에 위치하는지를 알 수 있다.
Viewport는 화면을 상대적인 크기로 나타내는 좌표 시스템이다.
가로축과 세로축은 0부터 1까지의 값을 가진다.
이때, 왼쪽 아래 모서리가 (0, 0)이고 오른쪽 위 모서리가 (1, 1)이다.
그래서 예를 들어, 아이템이 Viewport 좌표 (0.8, 0.2)에 위치한다면,
그 아이템은 오른쪽 위쪽으로 약간 이동한 위치에 있음을 알 수 있다.
이는 화면에 보이지 않는 위치이지만, Viewport 좌표를 통해 해당 위치를 확인할 수 있는 것이다.
즉, Viewport Point를 활용하면 화면에 표시되지 않는 객체가 어디에 위치하는지를 상대적인 좌표로 알 수 있다.
이를 통해 게임이나 앱에서 객체의 위치에 대한 정보를 활용할 수 있다.
Screen Point
: 카메라 내에서 오브젝트의 위치를 해상도를 기준으로 좌표를 매긴 값
1920 x 1080 해상도에서는 (0, 0) 부터 (1920, 1080) 까지 표현할 수 있다.
즉, World Point 값 x Viewport Point 값 = Screen Point 값이 된다.
Camera.ScreenToWorldPoint
Screen 좌표를 World 좌표로 변환한다. 마우스 클릭 위치 등의 Screen 좌표를 World 좌표로 변환할 때 유용하다.
- 예제 코드
: 화면내에서 클릭된 곳의 좌표를 가져올 수 있는데, 가져오는 값은 Screen Point이다.
이 좌표를 이용해서 이 위치에 오브젝트를 생성해주는 스크립트
using UnityEngine;
public class ObjectCreator : MonoBehaviour
{
[SerializeField] private GameObject objectPrefab;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 clickPosition = Input.mousePosition;
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(clickPosition);
Instantiate(objectPrefab, worldPosition, Quaternion.identity);
}
}
}
Camera.ViewportToWorldPoint
Viewport 좌표를 World 좌표로 변환한다.
Viewport 좌표는 카메라 뷰의 상대적인 위치를 나타내며,
주로 카메라의 시야 영역 내에서 위치를 지정할 때 사용된다.
Vector3 viewportPosition = new Vector3(0.5f, 0.5f, 10f); // 중앙에 위치하는 Viewport 좌표
Vector3 worldPosition = Camera.main.ViewportToWorldPoint(viewportPosition);
Camera.WorldToScreenPoint
World 좌표를 Screen 좌표로 변환한다.
오브젝트의 위치를 Screen 좌표로 변환하여 UI 요소를 배치하거나 화면 위에 정보를 표시할 때 유용하다.
Vector3 objectPosition = transform.position;
Vector3 screenPosition = Camera.main.WorldToScreenPoint(objectPosition);
Camera.ScreenPointToRay
Screen 좌표를 Ray로 변환한다.
주로 Raycast 작업을 수행할 때 사용되며,
화면에서 마우스를 클릭한 위치에 Ray를 쏘아 충돌 검사를 수행할 수 있다.
Vector3 mousePosition = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// 충돌한 오브젝트에 대한 처리
}
Camera.ViewportPointToRay
Viewport 좌표를 Ray로 변환한다.
Viewport 좌표를 Ray로 변환하여 Raycast 작업을 수행할 수 있다.
Vector3 viewportPosition = new Vector3(0.5f, 0.5f, 0f); // 중앙에 위치하는 Viewport 좌표
Ray ray = Camera.main.ViewportPointToRay(viewportPosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// 충돌한 오브젝트에 대한 처리
}
Camera.ScreenToViewportPoint
Screen 좌표를 Viewport 좌표로 변환한다.
Screen 좌표를 카메라의 Viewport 좌표로 변환하여 상대적인 위치를 계산할 수 있다.
Vector3 mousePosition = Input.mousePosition;
Vector3 viewportPosition = Camera.main.ScreenToViewportPoint(mousePosition);
Camera.WorldToViewportPoint
World 좌표를 Viewport 좌표로 변환한다.
오브젝트의 위치를 Viewport 좌표로 변환하여 상대적인 위치를 계산할 수 있다.
Vector3 objectPosition = transform.position;
Vector3 viewportPosition = Camera.main.WorldToViewportPoint(objectPosition);
Camera.ScreenToWorldPointOnPlane
특정 평면 위에서 Screen 좌표를 World 좌표로 변환한다.
주로 특정 평면과의 교차점을 계산할 때 사용된다.
Plane plane = new Plane(Vector3.up, Vector3.zero); // y = 0인 평면
Vector3 mousePosition = Input.mousePosition;
Vector3 worldPosition = Camera.main.ScreenToWorldPointOnPlane(mousePosition, plane);