orthographic 카메라의 nearClipPlane 값을 조절하면 단면효과를 낼 수 있다.
예)
이런식으로 효과 가능
스크립트 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Lv3_Cam : MonoBehaviour
{
public Slider m_slider; //nearClipPlane값을 조절하여 단면표현을 구현한다.
public Transform Target; //단면을 볼 타켓 오브젝트
Camera mainCam;
float rotX;
public float sensitivityX;
public float sensitivityY;
public Vector3 CamCenter;
[Header("카메라 영역 설정")]
float minX;
float minY;
float maxX;
float maxY;
float vertExtent;
float horzExtent;
// Start is called before the first frame update
void Start()
{
mainCam = Camera.main;
m_slider.onValueChanged.AddListener(delegate { Zoom_TP(m_slider.value); });
SetBound();
}
public void SetBound()
{
float vertExtent = mainCam.orthographicSize;
float horzExtent = vertExtent * Screen.width / Screen.height;
float verticalHeightSeen = Camera.main.orthographicSize * 2.0f;
// Calculations assume map is position at the origin
minX = horzExtent - (verticalHeightSeen * Camera.main.aspect);
maxX = (verticalHeightSeen * Camera.main.aspect) - horzExtent;
minY = vertExtent - verticalHeightSeen;
maxY = verticalHeightSeen - vertExtent;
}
public float RotationSpeed = 5;
public void Zoom_TP(float value)
{
mainCam.nearClipPlane = value;
}
// Update is called once per frame
void Update()
{
///UI 눌럿을때는 이동기능 하지 않게
if (isPointerOverUIObject(Input.mousePosition))
return;
#region 타겟 오브젝트 이동기능
if (Input.GetMouseButton(2))
{
rotX = Input.GetAxis("Mouse X") * RotationSpeed * Mathf.Deg2Rad * mainCam.orthographicSize;
Target.RotateAround(Vector3.up, -rotX);
}
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
mainCam.orthographicSize -= Input.GetAxis("Mouse ScrollWheel");
Setcenter();
}
///움직일수 있는 영역을 설정(정확하지는 않음)
if (Input.GetMouseButton(0))
{
float verticalHeightSeen = Camera.main.orthographicSize * 2.0f;
if (mainCam.transform.position.x < minX)
{
mainCam.transform.position = new Vector3(minX, mainCam.transform.position.y, mainCam.transform.position.z);
return;
}
if (mainCam.transform.position.x > maxX)
{
mainCam.transform.position = new Vector3(maxX, mainCam.transform.position.y, mainCam.transform.position.z);
return;
}
if (mainCam.transform.position.y < minY)
{
mainCam.transform.position = new Vector3(mainCam.transform.position.x, minY, mainCam.transform.position.z);
return;
}
if (mainCam.transform.position.y > maxY)
{
mainCam.transform.position = new Vector3(mainCam.transform.position.x, maxY, mainCam.transform.position.z);
return;
}
//if (mainCam.transform.position.x > minX)
// mainCam.transform.position = new Vector3(minX, mainCam.transform.position.y, mainCam.transform.position.z);
mainCam.transform.position -= mainCam.transform.right * Input.GetAxis("Mouse X") * sensitivityX * mainCam.orthographicSize;
mainCam.transform.position -= mainCam.transform.up * Input.GetAxis("Mouse Y") * sensitivityY * mainCam.orthographicSize;
}
#endregion
}
/// <summary>
/// 줌인을 할때마다 영역의 크기가 달라지므로 센터값을 변경해준다.
/// </summary>
void Setcenter()
{
CamCenter = mainCam.transform.position;
}
//Mouse가 UI 위에 있는지 확인하기위한 함수
public bool isPointerOverUIObject(Vector2 touchPos)
{
PointerEventData eventDataCurrentPosition
= new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = touchPos;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
return results.Count > 0;
}
//void OnDrawGizmos()
//{
// float verticalHeightSeen = Camera.main.orthographicSize * 2.0f;
// Gizmos.color = Color.cyan;
// Debug.Log("xSize: " + (verticalHeightSeen * Camera.main.aspect));
// Debug.Log("ySize: " + (verticalHeightSeen));
// Gizmos.DrawWireCube(transform.position, new Vector3((verticalHeightSeen * Camera.main.aspect), verticalHeightSeen, 0));
//}
}
댓글 없음:
댓글 쓰기