[Unity] Viewport 좌표
개발/Unity

[Unity] Viewport 좌표

출처: 인프런 [유니티(Unity)로 시작하는 게임개발: Part 3. 슈팅게임 개발] 슈팅게임 프로토타입 만들기 1 (캐릭터와 배경) : View Port와 월드좌표계

 

1. viewport 좌표계
카메라가 그릴 사각형의 화면을 기준으로 한 좌표계. 0에서 1사이의 값을 가지며, 왼쪽 아래가 (0,0)이다.

 

2. viewport 좌표계 -> world 좌표계로 변환

 

Unity - 스크립팅 API: Camera.ViewportToWorldPoint

Viewport space is normalized and relative to the camera. The bottom-left of the camera is (0,0); the top-right is (1,1). The z position is in world units from the camera. XY로 표현되는 스크린 위치를 3D 공간의 XYZ 위치로 변환하는 것에

docs.unity3d.com

Vector3 변환된 world coordinate position = 카메라 인스턴스.ViewportToWorldPoint(변환할 viewport position(Vector3))

// Camera.main: 메인 카메라 인스턴스를 가져옴
// new Vector(1,1,0): 뷰포트 우측 상단
Camera.main.ViewPortToWorldPoint(new Vector(1, 1, 0));

 

3. 왜 사용하는지

여러 해상도에 대응하는 프로그램을 만들기 위함 (16:9와 16:10 해상도 디스플레이에서 위 코드를 실행했을 때 결과가 다르게 나타남.)

'개발 > Unity' 카테고리의 다른 글

[Unity] 통과 감지  (0) 2021.09.07