摄像机的旋转缩放与跟随,并且保持跟随的时候不受人物的影响。
2016-01-31 20:26
477 查看
using UnityEngine; using System.Collections; public class MainFinger : MonoBehaviour { /// <summary> /// 用于绑定参照物体 /// </summary> public Transform target; /// <summary> /// 缩放系数 /// </summary> public float distance=10; /// <summary> /// 左右滑动移动速度 /// </summary> public float xSpeed = 250; public float ySpeed = 120; /// <summary> /// 缩放限制系数 /// </summary> public float yMinLimit = -20; public float yMaxLimit = 80; /// <summary> /// 摄像头的位置 /// </summary> public float x = 0; public float y = 0; /// <summary> /// 记录上一次手机触摸位置判断用户是在放大还是在缩小。 /// </summary> private Vector2 oldPosition1; private Vector2 oldPosition2; private string shang; /// <summary> /// 鼠标中轴的放大缩小 /// </summary> public float minDistance = 2; public float maxDistance = 10; public float mSpeed = 6; // Use this for initialization void Start () { var angles = transform.eulerAngles; x = angles.x; y = angles.y; } // Update is called once per frame void Update () { //判断触摸的数量为单点触摸。 distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed; Debug.Log(Input.GetAxis("Mouse ScrollWheel")); distance = Mathf.Clamp(distance, minDistance, maxDistance); if (Input.touchCount==1||Input.GetMouseButton(0)) { //触摸模型为移动触摸 if (Input.GetMouseButton(0)) { //根据触摸点计算x与y的位置 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y += Input.GetAxis("Mouse Y") * ySpeed * 0.02f; } if (Input.GetTouch(0).phase == TouchPhase.Moved) { //根据触摸点计算x与y的位置 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y += Input.GetAxis("Mouse Y") * ySpeed * 0.02f; } } //判断触摸数量为多点触摸 if (Input.touchCount>1) { shang = "双指"; //前两只手指触摸都为移动触摸 if (Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved) { shang = "爽直啊啊"; //计算出当前两点触摸点的位置 Vector2 tempPosition1 = Input.GetTouch(0).position; Vector2 tempPosition2 = Input.GetTouch(1).position; //返回真为放大,返回假为缩小 if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2)) { if (Camera.main.fieldOfView>2) { Camera.main.fieldOfView -= 2; } } if (!isEnlarge(oldPosition1,oldPosition2,tempPosition1,tempPosition2)) { if (Camera.main.fieldOfView<100) { Camera.main.fieldOfView += 5; } } oldPosition1 = tempPosition1; oldPosition2 = tempPosition2; } } } /// <summary> /// 函数返回真为放大,返回假为缩小 /// </summary> bool isEnlarge(Vector2 oP1,Vector2 oP2,Vector2 nP1,Vector2 nP2) { //函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势 var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y)); var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y)); if (leng1<leng2) { //放大的手势 return true; } else { //缩小的手势 return false; } } /// <summary> /// Update方法一旦调用结束后在这里算出重置摄像机的位置 /// </summary> void LateUpdate() { //target为缩放旋转的参照物 if (target) { //重置摄像机的位置 y = ClampAngle(y, yMinLimit, yMaxLimit); Quaternion rotation = Quaternion.Euler(y, x, 0); Vector3 position = rotation * new Vector3(0, 0, -distance) + target.position; transform.rotation = rotation; transform.position = position; } } float ClampAngle(float angle,float min,float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp(angle,min,max); }
相关文章推荐
- 【NanoPi2试用体验】eclipse使用的官方提供的交叉编译器
- HDOJ 2199 Can you solve this equation?
- 首页图片滚动效果
- NSDate计算时间差
- 大数据系列文章汇总 - 更新到15篇
- Node.js安全清单
- 第三届蓝桥杯C/C++组第九题 足球比赛(概率论+随机数)
- servlet之getRealPath获取路径问题。
- String使用equals方法和==分别比较的是什么?(转)
- LNK 2005 error 函数定义也是定义!!
- eclipse编译maven项目出错- No compiler is provided in this environment. Perhaps you are running on a JRE ra
- CF 71A [字符串统计]
- php中的全局变量$_REQUEST
- 【NanoPi2试用体验】简单人脸识别-结项
- Merkle Training 笔记
- ActiveMQ两种模式PTP和PUB/SUB
- Nginx基础入门之uptream负载均衡常用配置项说明
- 【NanoPi2试用体验】nanopi2下的二维码识别
- Using View and Data API with Meteor
- 博客转站