一:方法一

适用场景:三维场景中,需要自由、全方位观察模型
描述:通过WASD控制相机的移动,鼠标控制相机的旋转,相机正视的方向即为正前方,按W前进,S后退,A左边平移,D右边平移。滚轮控制视野的放大缩小。
相机需要加一个碰撞器,加碰撞器是为了限制相机在一个范围内移动,然后还需要把相机的可移动范围也用碰撞器包围起来,需要注意的是,不要用一个整的大的碰撞器,这样的话可能会把相机给弹飞,最好还是每一个面都加一个碰撞体,还有就是相机最好加球形碰撞体。
相机使用的事透视模式。
然后将下面脚本挂载在相机上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MouseLook : MonoBehaviour
{//public Transform character;public Transform _Camera;public float XSensitivity = 1.5f;public float YSensitivity = 1.5f;public bool clampVerticalRotation = false;public float MinimumX = -45F;public float MaximumX = 45F;public bool smooth = true;public float smoothTime = 8f;private Quaternion m_CharacterTargetRot;private Quaternion m_CameraTargetRot;private bool isMove = true;private void Start(){Init();}private void Update(){//if (Input.GetMouseButton(1))//{//    LookRotation(_Camera);//}LookRotation(_Camera);float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Vector3 dir = transform.forward * v + transform.right * h;transform.GetComponent<Rigidbody>().velocity = dir * 20;if (Input.GetAxis("Mouse ScrollWheel") != 0){//限制size大小transform.GetComponent<Camera>().fieldOfView = Mathf.Clamp(transform.GetComponent<Camera>().fieldOfView, 10, 75);//滚轮改变transform.GetComponent<Camera>().fieldOfView =transform.GetComponent<Camera>().fieldOfView - Input.GetAxis("Mouse ScrollWheel") * 20;}}public void Init(){//m_CharacterTargetRot = character.localRotation;m_CameraTargetRot = _Camera.localRotation;}public void LookRotation(Transform _camera){float yRot = Input.GetAxis("Mouse X") * XSensitivity;float xRot = Input.GetAxis("Mouse Y") * YSensitivity;m_CameraTargetRot = _camera.localRotation * Quaternion.Euler(-xRot, yRot, 0f);if (clampVerticalRotation)m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot);if (smooth){_camera.localRotation = Quaternion.Slerp(_camera.localRotation, m_CameraTargetRot,smoothTime * Time.deltaTime);}else{_camera.localRotation = m_CameraTargetRot;}_camera.localRotation = Quaternion.Euler(new Vector3(_camera.eulerAngles.x, _camera.eulerAngles.y, 0f));}Quaternion ClampRotationAroundXAxis(Quaternion q){q.x /= q.w;q.y /= q.w;q.z /= q.w;q.w = 1.0f;float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);angleX = Mathf.Clamp(angleX, MinimumX, MaximumX);q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);return q;}}

具体组件、参数设置如下图所示:

二:方法二

适用场景:二维场景中,需要自由、全方位观察模型
描述:通过WASD控制相机的移动,相机正视的方向即为正前方,按W向上移动,S向下移动,A左边平移,D右边平移。滚轮控制视野的放大缩小。
通过相机的位置坐标来控制相机的移动范围
相机使用的是正交模式。
然后将下面脚本挂载在相机上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;//巡游模式摄像机控制
public class CameraController : MonoBehaviour
{private Vector3 dirVector3;private Vector3 rotaVector3;[SerializeField]private float paramater = 1f;private float dis = 1;//相机缩放范围public int MinOrthographicSize=10;public int MaxOrthographicSize = 120;//相机移动的范围public Vector2 MoveX=new Vector2(-215f, 85f);public Vector2 MoveY = new Vector2(-75f, 92f);void Awake(){}private void Start(){}private void Update(){//移动dirVector3 = Vector3.zero;if (Input.GetKey(KeyCode.W)||Input.GetKey(KeyCode.UpArrow)){dirVector3.y = 1;}if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)){dirVector3.y = -1;}if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)){dirVector3.x = -1;}if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)){dirVector3.x = 1;}if (Input.GetAxis("Mouse ScrollWheel") != 0){//限制size大小transform.GetComponent<Camera>().orthographicSize = Mathf.Clamp(transform.GetComponent<Camera>().orthographicSize, MinOrthographicSize, MaxOrthographicSize);//滚轮改变transform.GetComponent<Camera>().orthographicSize =transform.GetComponent<Camera>().orthographicSize - Input.GetAxis("Mouse ScrollWheel") * 20;}         //在不同的缩放下  相对的降低相机的移动速度 if (80< transform.GetComponent<Camera>().orthographicSize&&transform.GetComponent<Camera>().orthographicSize<100){           paramater = 0.8f;        }if (50 < transform.GetComponent<Camera>().orthographicSize && transform.GetComponent<Camera>().orthographicSize < 80){paramater = 0.5f;}if (30 < transform.GetComponent<Camera>().orthographicSize && transform.GetComponent<Camera>().orthographicSize < 50){paramater = 0.25f;}if (10 < transform.GetComponent<Camera>().orthographicSize && transform.GetComponent<Camera>().orthographicSize < 30){paramater = 0.15f;}if ( transform.GetComponent<Camera>().orthographicSize < 10){paramater = 0.05f;}transform.Translate(dirVector3 * paramater, Space.Self);//限制摄像机范围float TempX = Mathf.Clamp(transform.position.x, MoveX.x, MoveX.y);float TempY = Mathf.Clamp(transform.position.y, MoveY.x,MoveY.y);transform.position = new Vector3(TempX, TempY, 30);}// Update is called once per framevoid FixedUpdate(){}
}

具体组件、参数设置如下图所示:

Unity相机自由观察场景模型方法总结相关推荐

  1. Unity中国古风仙侠场景模型、人物模型,217个场景+全套角色带动作

    Unity中国古风仙侠场景模型 人物模型,217个场景 资源介绍: 适用于unity4.6及以上版本,217个古风仙侠场景加人物模型带动作,适合unit初学者,来练习和研究学习,本素材不可商用,仅供学 ...

  2. Unity 使用陀螺仪观察场景

    Unity 使用陀螺仪观察场景 using UnityEngine;using UnityEngine.UI;public class CamMove : MonoBehaviour{private ...

  3. Unity 3D模型展示框架篇之自由观察(Cinemachine)

    本项目将整合之前Unity程序基础小框架专栏在Unity 3D模型展示项目基础上进行整合,并记录了集成过程中对原脚本的调整过程.增加了Asset Bundle+ILRuntime热更新技术流程. 在U ...

  4. Unity场景模型优化技术--LOD和OcclusionCulling

    LOD和Occlusion Culling Lod和遮挡剔除. Occlusion Culling:Occlusion Culling 技术是指当一个物体被其他物体遮挡住而相对当前摄像机为不可见时,可 ...

  5. unity相机围绕模型转_围绕我们的业务模型和风险进行安全测试

    unity相机围绕模型转 When we create an environment and consider our security testing from development to pro ...

  6. Cinemachine 5.自由观察相机(FreeLook)和状态驱动相机(State-Driven)

    自由观察相机(FreeLook) 创建FreeLook相机,并设置Follow和Look At,场景中会出现三个圆和一条弧线,这是用来控制摄像机的移动轨道,上下移动鼠标摄像机只能在上下两个圆之间移动. ...

  7. 领域适配前沿研究——场景、方法与模型选择

    下期内容:岂凡超<义原知识库的应用和扩充> 报名请点击「阅读原文」 在线直播时间:12月31日19:30-20:30 12月24日, 在智源论坛Live第2期活动中,我们邀请了2019年清 ...

  8. “智源论坛Live”报名 | 清华大学游凯超:领域适配前沿研究--场景、方法与模型选择...

    报名请点击「阅读原文」 在线直播时间:12月24日19:30-20:30 "智源论坛Live"第2期 "智源论坛Live"是智源论坛系列活动之一,通过在线直播形 ...

  9. Unity中如何给你的场景模型mesh减面——【一】

    一.前言 甲方给你一大场景,光fbx文件就4个G,导入Unity后,发现全部卡帧,变成动画片,发布成webGL足足有500多M.要使帧率至少达到60以上吧,发布成webGL不能超过500M,如何拯救呢 ...

最新文章

  1. 达摩院浙大上海人工智能实验室推出洛犀平台:大小模型端云协同进化
  2. iOS GCD_1
  3. PMcaff-运营 | 用户运营中的认知丶考虑丶行动模型
  4. 第四范式陈雨强:做机器学习平台天然就是新基建丨新基建50人
  5. linux命令行变大,Linux命令行下'!'的8大神奇的用法!
  6. js 实现简单的轮询
  7. 一篇极好的 CSS 教程
  8. 设计模式16_策略模式
  9. java循环打印三角形_Java for循环打印三角形(基础)
  10. 淘宝面试题:小白鼠与毒药
  11. 雷达散射截面(RCS)
  12. php毕设周记_php实习日记
  13. 如何在图片上快速添加文字水印?
  14. 怎么分割视频,将视频自定义分割成多个小片段的方法
  15. 最新四川大学软件工程课程期末官方复习知识点提纲
  16. qt中将按钮指向的鼠标变成手型
  17. 如图标黄的是什么意思?
  18. 电脑如何查看如何无限WiFi密码?
  19. kettle配置资源库
  20. Spring Boot MongoDB 实现总结

热门文章

  1. Go语言:谷歌google的uuid模块的基本用法
  2. 待定系数法求逻辑代数(函数)最简与或表达式
  3. 2021年3月电子学会Python等级考试试卷(三级)考题解析
  4. MySQL安装方法,亲测可用
  5. mysql5.7.16安装 初始密码获取及密码重置
  6. 机器学习与数据挖掘 课程作业 基于数据驱动的空调结霜程度检测方法研究
  7. UWB高精度室内定位--室内定位--新导智能
  8. Simulink HDL Coder FPGA开发实践之 基本使用流程介绍
  9. 【读书笔记】单人FPS关卡设计模式
  10. Win10主题更改后无法变更背景怎么解决