本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6856374.html

首先,总结了下最近工作中关于摄像机漫游的功能,

脚本如下:

     Transform _Camera;
public LayerMask mask; public float checkHeight = 500f;
public float minHeight = 20f;
public float maxHeight = 8000f;
public float minClamp = 50f;
public float maxClamp = 950f; public float sensitivityX = 5f;
public float sensitivityY = 5f;
private float rotationY = 0f; //上下最大Y视角
public float minimumY = -90f;
public float maximumY = 30f; public Vector3 PreMouseMPos; public float scrollSpeed = 200f; void Start ()
{
mask.value = ;
_Camera = Camera.main.transform;
} // Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.LeftAlt))
{
MouseScrollWheel ();
CameraMove ();
MoveEulerAngles ();
}
} /// <summary>
/// Checks the height of the low.
/// </summary>
void CheckLowHeight()
{
if (_Camera.position.x < minClamp)
_Camera.position = new Vector3 (minClamp, _Camera.position.y, _Camera.position.z);
if (_Camera.position.x > maxClamp)
_Camera.position = new Vector3 (maxClamp, _Camera.position.y, _Camera.position.z);
if (_Camera.position.z < minClamp)
_Camera.position = new Vector3 (_Camera.position.x, _Camera.position.y, minClamp);
if (_Camera.position.z > maxClamp)
_Camera.position = new Vector3 (_Camera.position.x, _Camera.position.y, maxClamp); RaycastHit hit;
if(Physics.Raycast(_Camera.position+Vector3.up*checkHeight,Vector3.down,out hit ,checkHeight+minHeight,mask))
{
if(_Camera.position.y-hit.point.y<minClamp)
{
Vector3 lowPoint = hit.point + new Vector3 (, minHeight, );
_Camera.position = lowPoint;
}
}
} /// <summary>
/// Mouses the scroll wheel.
/// </summary>
private void MouseScrollWheel()
{
if(Input.GetAxis("Mouse ScrollWheel")!=)
{
_Camera.Translate (new Vector3 (, , Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * scrollSpeed));
} CheckLowHeight (); if (_Camera.position.y >= maxHeight)
_Camera.position = new Vector3 (_Camera.position.x, maxHeight, _Camera.position.z);
} /// <summary>
/// Cameras the move.
/// </summary>
private void CameraMove()
{
if(Input.GetMouseButton())
{
if(PreMouseMPos.x<=)
{
PreMouseMPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, );
}
else
{
Vector3 CurMouseMPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, );
Vector3 offset = CurMouseMPos - PreMouseMPos;
offset = -offset * 0.5f * ;
if((_Camera.position+offset).y>=minHeight
&&(_Camera.position+offset).y<=maxHeight)
{
_Camera.Translate(offset); CheckLowHeight(); PreMouseMPos = CurMouseMPos;
}
}
}
else
PreMouseMPos = Vector3.zero;
} /// <summary>
/// Moves the euler angles.
/// </summary>
private void MoveEulerAngles()
{
if(Input.GetMouseButton())
{
float rotationX = _Camera.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX; rotationY += Input.GetAxis ("Mouse Y") * sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); _Camera.localEulerAngles = new Vector3 (-rotationY, rotationX, );
}
}

MoveRocket

第二个是关于摄像机的限制范围和遇到障碍物自动变换与target的距离的功能

代码如下:

 public Transform target;

     public LayerMask mask = new LayerMask();

     public Vector2 targetOffset = new Vector2();
public Vector2 originRotation = new Vector2(); public float distance = ;
public float minDistance = ;
public float maxDistance = ; public Vector2 sensitivity = new Vector2(, ); public float zoomSpeed = ;
public float zoomSmoothing = ; public float minAngle = -;
public float maxAngle = ; private float _zoom_in_timer = ;
private float _zoom_out_timer = ; private float _wanted_distance;
private Quaternion _rotation;
private Vector2 _input_rotation; private Transform _t; void Start()
{
mask.value = ;
_t = transform;
_wanted_distance = distance;
_input_rotation = originRotation; } void Update()
{
if (target) { if (Input.GetMouseButton () || Input.GetMouseButton ()) {
if (Input.GetAxis ("Mouse X") != || Input.GetAxis ("Mouse Y") != ) {
if (!Cursor.visible) {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
} return;
}
} if (!Cursor.visible) {
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
} }
void FixedUpdate()
{
if(target)
{ // Zoom control
if(Input.GetAxis("Mouse ScrollWheel") < )
{
_wanted_distance += zoomSpeed;
}
else if(Input.GetAxis("Mouse ScrollWheel") > )
{
_wanted_distance -= zoomSpeed;
} _wanted_distance = Mathf.Clamp(_wanted_distance, minDistance, maxDistance); if (Input.GetMouseButton () || Input.GetMouseButton ()) { _input_rotation.x += Input.GetAxis ("Mouse X") * sensitivity.x; ClampRotation (); _input_rotation.y -= Input.GetAxis ("Mouse Y") * sensitivity.y; _input_rotation.y = Mathf.Clamp (_input_rotation.y, minAngle, maxAngle); _rotation = Quaternion.Euler (_input_rotation.y, _input_rotation.x, ); } // Lerp from current distance to wanted distance
distance = Mathf.Clamp(Mathf.Lerp(distance, _wanted_distance, Time.deltaTime * zoomSmoothing), minDistance, maxDistance); // Set wanted position based off rotation and distance
Vector3 wanted_position = _rotation * new Vector3(targetOffset.x, , -_wanted_distance - 0.2f) + target.position + new Vector3(, targetOffset.y, );
Vector3 current_position = _rotation * new Vector3(targetOffset.x, , ) + target.position + new Vector3(, targetOffset.y, ); // Linecast to test if there are objects between the camera and the target using collision layers
RaycastHit hit; if(Physics.Linecast(current_position, wanted_position, out hit, mask))
{
distance = Vector3.Distance(current_position, hit.point) - 0.2f;
} // Set the position and rotation of the camera
_t.position = _rotation * new Vector3(targetOffset.x, 0.0f, -distance) + target.position + new Vector3(, targetOffset.y, );
_t.rotation = _rotation;
}
} private void ClampRotation()
{
if(originRotation.x < -)
{
originRotation.x += ;
}
else if(originRotation.x > )
{
originRotation.x -= ;
} if(_input_rotation.x - originRotation.x < -)
{
_input_rotation.x += ;
}
else if(_input_rotation.x - originRotation.x > )
{
_input_rotation.x -= ;
}
}

Unity C# 一些关于Camera的心得!的更多相关文章

  1. [Unity] How to stop camera rendering?

    http://answers.unity3d.com/questions/147988/how-to-pause-the-main-camera-.html I would simply pause ...

  2. Unity 3D 之贪吃蛇 Text 心得 & Audio

    当我们需要在游戏街面上增加文本时, 我们就需要用到Text 组件 注意: 当文本的长度或者宽度不够时,字体将无法显示. 因为是面对组件编程,所以每一个组件的component都可以同过GetCompo ...

  3. 一次UNITY闪退问题的定位心得

    最近项目测试发现,运行unity后不退出运行模式,玩了一局后点击 “再来一局”,反复十几局后unity崩掉. 经观察,发现在这十几局的过程中,unity占用内存不断上升,由3.2G左右上升到3.6G左 ...

  4. Unity仪表盘显示UGUI制作小心得

    最近在做设备仪表参数参数显示,由于模型摆放位置经常修改,加之要求不能在模型的下面添加东西,显示界面的位置也不得不跟着修改,一来二去就烦了,想了解决办法,现在总结如下: 1.仍然在模型下面新建Panel ...

  5. 制作和unity调用动态链接库dll文件

    首先用vc建立一个dll工程 然后在里面建立一个testunity.h文件.内容如下 1 extern "C" int _declspec(dllexport)testunity( ...

  6. [原]Unity3D深入浅出 - 摄像机组件(Camera)

    在Unity中创建一个Camera后,除了默认带一个Transform组件外,还会附带Flare Layer.GUI Layer.Audio Listener等4个组件,如下图. ClearFlags ...

  7. Unity 坐标 转换 详解 World世界坐标 Screen屏幕坐标 View视口坐标 GUI坐标 NGUI坐标 localPosition相对父级坐标

    在制作游戏中我们经常会遇到这样一个需求: 在人物模型的上面显示 名字.称号 一类的文字或者图片 如下图 人物模型属于是Camera1   UI Title信息属于NGUI Camera2 如下图 这时 ...

  8. Unity和Android混合开发

    通用的流程 https://blog.csdn.net/zhangdi2017/article/details/65629589 应用场景 Unity游戏中一些功能需要安卓系统的支持,如搜索wifi等 ...

  9. Unity事件处理机制与NGUI事件机制

    1 Unity原生 1.1 GUI void OnGUI(){ if(GUI.Button(Rect position, string text)){ //点击后立即执行 } 1.1 Input 每个 ...

随机推荐

  1. Liniux系统下目录的权限意义

    访问者及其基本权限 Linux系统内的文件访问者有三种身份,分别是: a) 文件和文件目录的所有者: u---User(所有权);b) 文件和文件目录的所有者所在的组的用户: g---Group;c) ...

  2. linux 下日常使用便利工具

    Nautilus 你工作中有在GUI和命令行之间切来切去吗?当你总是要在命令行中输入你要进入的目录的时候,你有沮丧无奈过吗?如果有的话,那么,你一定要试下这个nautilus插件 —— nautilu ...

  3. ajax VS websocket

    一. ajax VS websocket总结 http://blog.csdn.net/qiuhuanmin/article/details/50719114 二.用Websocket代替Ajax来开 ...

  4. 4.Java集合总结系列:Map接口及其实现

    一.Map接口 Map集合的特点是:通过key值找到对应的value值,key值是唯一的,value可以重复.Map中的元素是无序的,但是也有实现了排序的Map实现类,如:TreeMap. 上面Map ...

  5. Java集合常见面试题集锦

    1.介绍Collection框架的结构 集合是Java中的一个非常重要的一个知识点,主要分为List.Set.Map.Queue三大数据结构.它们在Java中的结构关系如下: Collection接口 ...

  6. 在hive中直接对timestamp类型取max报错

    之前直接对timestamp类型做max操作, select id,max(updatetime) updatetime from his.tag group by id; 结果查询的结果有的显示为1 ...

  7. Centos 在 Xshell里 vim的配置

    Centos里的VI只默认安装了vim-minimal-7.x.所以无论是输入vi或者vim查看文件,syntax功能都无法正常启用.因此需要用yum安装另外两个组件:vim-common-7.x和v ...

  8. Eclipse中Maven插件部分常用功能命令介绍

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6689010.html Eclipse中安装Maven插件之后,就能很方便的管理Maven项 ...

  9. Java关于e.printStackTrace()介绍

    public void printStackTrace()将此 throwable 及其追踪输出至标准错误流.此方法将此 Throwable 对象的堆栈跟踪输出至错误输出流,作为字段 System.e ...

  10. html5脚本编程

    (1)跨文档消息传递,XDM.指的是来自不同域的页面间传递消息. XDM的核心是postMessage();向另一个地方传递数据,指是包含在当前页面中的iframe元素,由当前页面弹出的窗口. var ...