本文原创,转载请注明出处: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. MVC学习笔记3 - JsRender

    许多发展平台减少代码和简化维护,使用模板和 HTML5 和 JavaScript 也不例外. JsRender 是一个 JavaScript 库使您可以一次定义一个样板文件结构,并使用它来动态地生成 ...

  2. [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(二)

    一步步打造一个简单的 MVC 电商网站 - BooksStore(二) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore 前: ...

  3. poptest老李谈jvm的GC

    poptest老李谈jvm的GC   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90882 ...

  4. 11.Linux用户管理

    本笔记说的Linux用户管理主要包括以下内容: 1.用户配置文件(/etc/passwd  /etc/shadow) 2.组配置文件(/etc/group  /etc/gshadow) 3.用户缺省配 ...

  5. ViewPager—02图片无限轮播

    布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  6. 通过UDP广播实现Android局域网Peer Discovering

    本文是对个人笔记中内容的整理,部分代码及图片来自互联网,由于不好找到原始出处,所以未加注明. 如有痛感,联系删除. 本文将介绍以下知识点: TCP与UDP的区别: 单播.多播.广播: Java中实现U ...

  7. select下拉二级联动

    function opt(){ var id = $("#ids").val(); $.ajax({   type: "POST",  url: "$ ...

  8. 【模板】Tarjan求强连通分量

    有人说这篇博客不是很友好,所以我加了点解释,感觉是不是友好多了? dfn[u]表示节点u在dfs时被访问的次序. low[u]表示节点u能够追溯到的最远的祖先的dfn. ins[u]表示节点u是否在栈 ...

  9. seajs加载angularjs

    angularjs是自动完成模块的控制的,而seajs加载模块是异步的,所以不做修改,直接seajs加载angularjs会出错.            在这里讲下自己的解决方法 一.需要把ng-ap ...

  10. ArrayList 进阶方法之ListIterator

    同样看的都是jdk1.8 中 ArrayList中的源码,整理测试一下而已ListIterator(int index)方法,返回指定下标(包含该下标)后的值,此时index位置的元素就是新列表迭代器 ...