using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class FreedomCamera : MonoBehaviour
{
private bool isDown = false;
public float speed = 0.6f;
public float distance = 10;
public Vector3 centerPoint = Vector3.zero;
public float fov = 60;
public UnityAction<Vector3> unityAction;
private Vector3 rotation = new Vector3(50,0,0);
private Vector3 downPos = Vector3.zero;
private Vector3 downRot = Vector3.zero;
private float lastClickTime;
private Camera currentCamera;
private void Awake()
{
currentCamera = this.GetComponent<Camera>();
}
private void Update()
{
if (!EventSystem.current.IsPointerOverGameObject())
{
if (Input.GetMouseButtonDown(0))
{
isDown = true;
downPos = Input.mousePosition;
downRot = rotation;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
distance = Mathf.Clamp(distance - 1f, 0, 500);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
distance = Mathf.Clamp(distance + 1f, 0, 500);
}
}
if (Input.GetMouseButtonUp(0))
{
isDown = false;
if (!EventSystem.current.IsPointerOverGameObject())
{
if (currentCamera != null)
{
float nowTime = Time.time;
if (nowTime - lastClickTime < 0.3)
{
int layer = LayerMask.GetMask("Terrain");
RaycastHit hitInfo;
if (Physics.Raycast(currentCamera.ScreenPointToRay(Input.mousePosition), out hitInfo, 1000, layer))
{
centerPoint = hitInfo.point;
if (unityAction != null)
{
unityAction.Invoke(centerPoint);
}
}
}
lastClickTime = nowTime;
}
}
}
if (isDown == true)
{
var dis = (Input.mousePosition - downPos) * speed;
rotation = new Vector3(downRot.x - dis.y, downRot.y + dis.x, downRot.z + dis.z);
}
FollowTarget();
}
void FollowTarget()
{
var r = Quaternion.Euler(rotation);
Vector3 fixPos = r * Vector3.forward * distance;
transform.position = centerPoint - fixPos;
transform.LookAt(centerPoint);
currentCamera.fieldOfView = fov;
}
}
相機(jī)圍繞某個(gè)點(diǎn)自由視角
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 本節(jié)學(xué)習(xí)目標(biāo) 檢測(cè)相機(jī)視野中有哪些節(jié)點(diǎn)2.判斷節(jié)點(diǎn)是否在相機(jī)視野中 比如ar導(dǎo)航項(xiàng)目中經(jīng)常會(huì)檢測(cè)某個(gè)節(jié)點(diǎn)是否在視野...
- 最近做項(xiàng)目的時(shí)候出現(xiàn)的需求,需要機(jī)動(dòng)車(chē)在高速剎車(chē)的時(shí)候相機(jī)視角出現(xiàn)延遲的感覺(jué),我試了論壇上很多中相機(jī)跟隨的辦法,都...
- 使用的時(shí)候感覺(jué)手感不好:1.已修改鼠標(biāo)控制旋轉(zhuǎn)的時(shí)候平滑處理。2.已添加相機(jī)的放大和縮小功能3.已添加功能,在已存...
- 在2018版本以上后,unity集成了虛擬相機(jī)插件,利用插件可以相機(jī)做跟隨,注視,限制區(qū)域,已經(jīng)軌道相機(jī),鏡頭切換...
- 將自己塑造成為一臺(tái)更好的相機(jī)。多角度拍攝。讓自己多角度看問(wèn)題,避免焦慮。 1、勤移動(dòng)。多移動(dòng)“相機(jī)”機(jī)位,嘗試用不...