控制场景相机来观察物体
写这篇主要是为了记录一下用到的东西,也方便以后进行回顾
1.首先新建两个空物体,按照图片子父物体顺序摆好,CamerRoot是控制相机旋转,CamerTF控制相机的远近,主相机参数不要改变
2.新建脚本Camer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public struct Cameraparameter//定义相机参数
{
public bool limitXAngle;//限制在X轴上的角度;
public float minXAngle;//角度的最小值
public float maxXAngle;//角度最大值
public bool limitYAngle;//限制在Y轴上的角度;
public float minYAngle;//角度的最小值
public float maxYAngle;//角度最大值
public float orbitScensitive;//当相机停止是,相机移动的惯性值
public float mouseMoveRatio;//相机移动的速率
public Cameraparameter(bool limitXAngle = true,
float minXAngle = 0f,
float maxXAngle = 80f,
bool limitYAngle = false,
float minYAngle = 0f,
float maxYAngle = 0f,
float orbitScensitive = 10f,
float mouseMoveRatio = 0.3f
)
{
this.limitXAngle = limitXAngle;
this.minXAngle = minXAngle;
this.maxXAngle = maxXAngle;
this.limitYAngle = limitYAngle;
this.minYAngle = minYAngle;
this.maxYAngle = maxYAngle;
this.orbitScensitive = orbitScensitive;
this.mouseMoveRatio = mouseMoveRatio;
}
}
public class Camer : MonoBehaviour
{
private Vector3 lastMousePos;//鼠标点下的位置;
private Vector3 targetEularAngle;//旋转角
private Vector3 eulurAngle;
public Cameraparameter freeOrbitParameter;//自由旋转时相机的参数
private Cameraparameter cureenCamerParameter;//当前相机参数
public Transform cameraRootTf;//摄像机根节点;
public Transform cameraTf;//相机
private float cameraDistance;//相机本身距离
private float targetCamerDistance;//相机距离目标的距离
private float lastTouchDistance;//两指之间的距离;
public float minDistance = 5;//相机推进的最小距离
public float maxDistance = 30f;//相机推进的最大距离
public float mouseScroollRatio = 1f;//鼠标滚轮的速率
public float zoomSensitive = 1;//相机推进的灵敏度
private bool IsRastion = true;
public float Speed = 5f;
private Quaternion originalRotate; //旋转的欧拉角
private void Start()
{
originalRotate = cameraRootTf.rotation;
cameraDistance = cameraTf.localPosition.z;
targetCamerDistance = cameraDistance;
cureenCamerParameter = freeOrbitParameter;
//OpenCheck();
}
private void Update()
{
if (Input.touchCount > 0 || Input.GetMouseButtonDown(0))
{
Speed = 0f;
IsRastion = false;
}
Oribit();
Zoom();
}
private void Oribit()
{
if (Input.GetMouseButtonDown(0))
{
lastMousePos = Input.mousePosition;//鼠标一开始点击的位置
}
if (Input.GetMouseButton(0))
{
targetEularAngle.x += (-Input.mousePosition.y + lastMousePos.y) * cureenCamerParameter.mouseMoveRatio;//获取水平旋转的角度
targetEularAngle.y += (Input.mousePosition.x - lastMousePos.x) * cureenCamerParameter.mouseMoveRatio;//获取垂直旋转的角度
if (cureenCamerParameter.limitXAngle)//限制X轴上的旋转角度
{
targetEularAngle.x = Mathf.Clamp(targetEularAngle.x, cureenCamerParameter.minXAngle, cureenCamerParameter.maxXAngle);
}
if (cureenCamerParameter.limitYAngle)//限制Y轴上的旋转角度
{
targetEularAngle.y = Mathf.Clamp(targetEularAngle.y, cureenCamerParameter.minYAngle, cureenCamerParameter.maxYAngle);
}
lastMousePos = Input.mousePosition;
}
if (Input.touchCount < 2 && !IsRastion)
{
eulurAngle = Vector3.Lerp(eulurAngle, targetEularAngle, cureenCamerParameter.orbitScensitive);
cameraRootTf.rotation = originalRotate * Quaternion.Euler(eulurAngle);
}
}
private void Zoom()//相机推进的方法
{
if (Input.touchCount < 2)
{
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
cameraDistance = -cameraTf.localPosition.z;
targetCamerDistance = cameraDistance - Input.GetAxis("Mouse ScrollWheel") * cameraDistance * mouseScroollRatio;
targetCamerDistance = Mathf.Clamp(targetCamerDistance, minDistance, maxDistance);
}
}
else
{
if (Input.GetTouch(1).phase == TouchPhase.Began)
{
lastTouchDistance = Vector2.Distance(Input.GetTouch(1).position, Input.GetTouch(0).position);
}
if (Input.GetTouch(1).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Moved)
{
cameraDistance = -cameraTf.localPosition.z;
targetCamerDistance = cameraDistance - (Vector2.Distance(Input.GetTouch(1).position, Input.GetTouch(0).position) - lastTouchDistance);
//lastMousePos = Input.mousePosition;
targetCamerDistance = Mathf.Clamp(targetCamerDistance, minDistance, maxDistance);
}
}
if (Mathf.Abs(targetCamerDistance - cameraDistance) > 0.1f)
{
cameraDistance = Mathf.Lerp(cameraDistance, targetCamerDistance, zoomSensitive);
cameraTf.localPosition = new Vector3(0f, 0f, -cameraDistance);
}
}
}
cameraTf.localPosition = new Vector3(0f, 0f, -cameraDistance)
注意这里Vector的参数Y值要和CamerTf的Poistion的Y值保持一致。
3.最后直接把新建的脚本直接挂在CamerRoot上
这里参数根据自己需要调整。
注:这些代码是参考B站博主视屏,不懂得可以去看一下,也可以仔细看一下代码,可以看得懂 ;最后大家加油啊 Developer殿堂 一个很棒的博主