这篇文章主要讲解了Unity3D实现摄像机镜头移动并限制角度的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

摄像机镜头跟随鼠标移动,并限制上下左右的移动角度
public class ViewFromCream : MonoBehaviour
{
public int speed=5;
public Vector3 vect;
private float xcream;
private float ycream;
public void Update()
{
CreamView();
}
private void CreamView()
{
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
if (x!=0||y!=0)
{
LimitAngle(60);
LimitAngleUandD(60);
this.transform.Rotate(-y * speed, 0, 0);
this.transform.Rotate(0, x * speed, 0, Space.World);
}
}
///
/// 限制相机左右视角的角度
///
/// 角度
private void LimitAngle(float angle)
{
vect = this.transform.eulerAngles;
//当前相机x轴旋转的角度(0~360)
xcream = IsPosNum(vect.x);
if (xcream > angle)
this.transform.rotation = Quaternion.Euler(angle,vect.y,0);
else if (xcream < -angle)
this.transform.rotation = Quaternion.Euler(-angle, vect.y, 0);
}
///
/// 限制相机上下视角的角度
///
///
private void LimitAngleUandD(float angle)
{
vect = this.transform.eulerAngles;
//当前相机y轴旋转的角度(0~360)
ycream = IsPosNum(vect.y);
if (ycream > angle)
this.transform.rotation = Quaternion.Euler(vect.x, angle, 0);
else if (ycream < -angle)
this.transform.rotation = Quaternion.Euler(vect.x, -angle, 0);
}
///
/// 将角度转换为-180~180的角度
///
///
///
private float IsPosNum(float x)
{
x -= 180;
if (x < 0)
return x + 180;
else return x - 180;
}
}另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。