본문 바로가기
[ Unity ]/- 유니티 실습

유니티 Camera Following

by MRG 2024. 9. 19.
728x90
반응형

 

▣ 코드 설명
player: 카메라가 따라다닐 플레이어의 Transform을 연결합니다.
minPosition, maxPosition: 카메라가 이동할 수 있는 범위를 지정합니다. minPosition은 최소 좌표, maxPosition은 최대 좌표입니다. 이 값들을 Mathf.Clamp로 제한하여 카메라가 해당 범위를 벗어나지 않게 합니다.
cameraFOV: 카메라의 시야각을 설정하는 변수입니다. 기본값으로 60도를 설정했으며, 필요에 따라 조정 가능합니다.
LateUpdate(): 플레이어의 움직임 이후 카메라가 자연스럽게 움직이도록 LateUpdate에서 카메라 위치를 업데이트합니다.

▣ 배경 오브젝트 크기 계산:
backgroundRenderer.bounds는 배경 오브젝트의 바운딩 박스를 가져옵니다. 

이 바운딩 박스는 배경 오브젝트의 경계를 포함합니다.
이 값을 이용해 카메라가 이동할 수 있는 최소 좌표(minPosition)와 최대 좌표(maxPosition)를 계산합니다. 

이때 카메라의 크기(가로, 세로)를 반영하여 배경에서 벗어나지 않도록 보정합니다.

▣ 카메라의 절반 크기 계산:
카메라가 Orthographic 모드일 경우 카메라의 절반 높이는 camera.orthographicSize로 계산되고, 

절반 너비는 aspect ratio를 곱해서 계산됩니다.
이 값을 이용해 카메라가 배경에서 벗어나지 않도록 최소 및 최대 좌표를 계산할 때 보정합니다.

 

▣  camera.orthographicSize란?
camera.orthographicSize는 카메라의 세로 반경을 의미합니다. 

이는 카메라가 세로 방향으로 얼마나 많은 공간을 보여주는지를 결정합니다.
중요한 점은 이 값이 화면의 절반 높이라는 것입니다. 

즉, orthographicSize는 카메라가 중앙에서 위쪽이나 아래쪽으로 보여줄 수 있는 거리입니다.
예를 들어, camera.orthographicSize가 5로 설정되면, 카메라는 위로 5 유닛, 아래로 5 유닛을 볼 수 있어, 총 10 유닛의 높이를 보게 됩니다.


▣ camera.aspect란?
camera.aspect는 카메라의 종횡비(aspect ratio)를 의미하며, 이 값은 화면의 너비를 높이로 나눈 값입니다.

예를 들어, 일반적인 16:9 화면에서는 camera.aspect가 약 1.7777 정도 됩니다.


▣ 카메라의 위치를 제한:
Mathf.Clamp를 사용하여 플레이어의 위치에 따라 카메라가 이동하되, 계산된 minPosition과 maxPosition 사이에서만 이동하도록 합니다.

▣ 카메라 시야각 설정:
Camera.main.fieldOfView를 사용해 카메라의 시야각을 설정합니다.

▣ Bounds backgroundBounds = backgroundRenderer.bounds;는

Unity에서 특정 오브젝트의 바운딩 박스(Bounding Box)를 가져오는 코드입니다.

이 코드는 오브젝트의 3D 공간에서 차지하는 영역(경계)을 정의합니다.

▣ bounds란?
Renderer.bounds는 해당 오브젝트가 화면에 표시될 수 있는 최소한의 직육면체 영역을 의미합니다. 

이 영역은 그 오브젝트의 모든 메쉬 또는 콜라이더를 포함하는 3D 공간에서의 경계를 말합니다. 쉽게 말해, 오브젝트를 둘러싸는 상자(경계 상자)를 뜻합니다.

 

 

 

https://docs.unity3d.com/ScriptReference/Renderer-bounds.html

 

Unity - Scripting API: Renderer.bounds

This is the axis-aligned bounding box fully enclosing the object in world space. Using bounds is convenient to make rough approximations about the object's location and its extents. For example, the center property is often a more precise approximation to

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/Bounds.html

 

Unity - Scripting API: Bounds

An axis-aligned bounding box, or AABB for short, is a box aligned with coordinate axes and fully enclosing some object. Because the box is never rotated with respect to the axes, it can be defined by just its center and extents, or alternatively by min and

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html

 

Unity - Scripting API: Camera.orthographicSize

The orthographicSize property defines the viewing volume of an orthographic Camera. In order to edit this size, set the Camera to be orthographic first through script or in the Inspector. The orthographicSize is half the size of the vertical viewing volume

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/Camera-aspect.html

 

Unity - Scripting API: Camera.aspect

By default the aspect ratio is automatically calculated from the screen's aspect ratio, even if the camera is not rendering to full area. If you modify the aspect ratio of the camera, the value will stay until you call camera.ResetAspect(); which resets th

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/Camera-fieldOfView.html

 

Unity - Scripting API: Camera.fieldOfView

This is the vertical field of view; horizontal field of view depends on the viewport's aspect ratio. For for more information, see VerticalToHorizontalFieldOfView. If Camera.orthographic is true, the Camera ignores fieldOfView. Some VR SDKs have fixed fiel

docs.unity3d.com

https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html

 

Unity - Scripting API: Mathf.Clamp

Returns the minimum value if the given float value is less than the minimum. Returns the maximum value if the given value is greater than the maximum value. Use Clamp to restrict a value to a range that is defined by the minimum and maximum values. Returns

docs.unity3d.com

 

728x90
반응형

댓글