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;
}
}
相机围绕某个点自由视角
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本节学习目标 检测相机视野中有哪些节点2.判断节点是否在相机视野中 比如ar导航项目中经常会检测某个节点是否在视野...
- 在2018版本以上后,unity集成了虚拟相机插件,利用插件可以相机做跟随,注视,限制区域,已经轨道相机,镜头切换...
- 将自己塑造成为一台更好的相机。多角度拍摄。让自己多角度看问题,避免焦虑。 1、勤移动。多移动“相机”机位,尝试用不...