总结圣典中操作物体任意方向旋转的三种方法
2016-06-14 21:55
676 查看
http://www.ceeger.com/forum/read.php?tid=10328
方法一:
另外加入了缩放的代码,缩放、自动旋转、拖拽物体的综合:
方法一:
using UnityEngine; using System.Collections; public class Rotate1 : MonoBehaviour { private Transform hitTransform; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { // 射线检测,单击鼠标是否单击到一个物体上 RaycastHit hit; Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(mouseray, out hit)) { hitTransform = hit.transform; } } else if(Input.GetMouseButtonUp(0)) { hitTransform = null; } if (hitTransform) { Matrix4x4 localmatrix = hitTransform.worldToLocalMatrix; // MultiplyVector() 通过这个矩阵变换方向 Vector3 vUp = localmatrix.MultiplyVector(new Vector3(0, 1, 0)); Vector3 vRight = -localmatrix.MultiplyVector(new Vector3(1, 0, 0)); // Quaternion.AngleAxis 角轴 //绕axis轴旋转angle,创建一个旋转。 //设置变换的旋转,绕y轴旋转30度 // for example transform.rotation = Quaternion.AngleAxis(30, Vector3.up); float fMoveX = -Input.GetAxis("Mouse X") * Time.deltaTime * 200.0f; Quaternion rotationX = Quaternion.AngleAxis(fMoveX, vUp); hitTransform.localRotation *= rotationX; float fMoveY = -Input.GetAxis("Mouse Y") * Time.deltaTime * 200f; Quaternion rotationY = Quaternion.AngleAxis(fMoveY, vRight); hitTransform.localRotation *= rotationY; } } }方法二:
using UnityEngine; using System.Collections; public class Rotate2 : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnMouseDrag() { this.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), -Input.GetAxis("Mouse X"), 0) * 6f, Space.World); } }方法三(带惯性的拖拽):
using UnityEngine; using System.Collections; public class Rotate3 : MonoBehaviour { private bool onDrag = false; private float axisX; private float axisY; public float speed = 6f; private float tempSpeed; // Use this for initialization void Start () { } // Update is called once per frame void OnMouseDrag() { onDrag = true; axisX = Input.GetAxis("Mouse X"); axisY = Input.GetAxis("Mouse Y"); } void Update () { transform.Rotate(new Vector3(axisY, -axisX, 0) * Rigid(), Space.World); if (!Input.GetMouseButtonDown(0)) { onDrag = false; } } float Rigid() { if (onDrag) { if (tempSpeed < speed) { tempSpeed += speed * Time.deltaTime * 5; } else tempSpeed = speed; }else { if (tempSpeed > 0) tempSpeed -= speed * Time.deltaTime; else tempSpeed = 0; } // return tempSpeed; } }
另外加入了缩放的代码,缩放、自动旋转、拖拽物体的综合:
/* * 使物体自动旋转,鼠标能够360°拖转物体,托转物体的时候, * 自动旋转停止,同时滚轮实现物体的缩放功能 * */ using UnityEngine; using System.Collections; public class ZoomAndDrag : MonoBehaviour { public Camera MainCamera; private bool RotationOnly; private float normalDistance; // 设置摄像机的景深数值 private float ZoomMin; // 滚轮的最小值 private float ZoomMax; // 滚轮的最大值 private float MouseWheelSencitivity = 10.0f; //鼠标灵敏度,就是缩放速度的快慢 private float axisX; private float axisY; public float speed = 6f; private float tempSpeed; // Use this for initialization void Start () { normalDistance = 50.0f; RotationOnly = true; ZoomMax = 100f; ZoomMin = 20f; } // Update is called once per frame void Update () { Rotation(); Zoom(); transform.Rotate(new Vector3(axisY, -axisX, 0) * Rigid(), Space.World); // 物体旋转 } // //自动旋转物体的方法,放在Update中调用 void Rotation() { if (RotationOnly) { gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 10); } } //*鼠标滚轮缩放物体的方法 void Zoom() { if (Input.GetAxis("Mouse ScrollWheel") != 0) { if(normalDistance >= ZoomMin && normalDistance <= ZoomMax) normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSencitivity; if (normalDistance < ZoomMin) normalDistance = ZoomMin; if (normalDistance > ZoomMax) normalDistance = ZoomMax; MainCamera.fieldOfView = normalDistance; } } /// <summary> /// 鼠标左键控制物体360°旋转 + 惯性 /// </summary> /// <returns></returns> float Rigid() { if (Input.GetMouseButton(0)) { print("nishishui"); RotationOnly = false; //当鼠标按下的时候,停止自动旋转 axisX = Input.GetAxis("Mouse X"); axisY = Input.GetAxis("Mouse Y"); if (tempSpeed < speed) tempSpeed += speed * Time.deltaTime * 5; else tempSpeed = speed; } else { RotationOnly = true; //当鼠标没有按下的时候,恢复自动旋转 if (tempSpeed > 0) tempSpeed -= speed * Time.deltaTime; else tempSpeed = 0; } return tempSpeed; } }
相关文章推荐
- 增加eclipse启动的Tomcat内存的方法 tomcat内存增加
- leetcode-java-58. Length of Last Word
- 统计学 分布篇 - Normal/Gaussian Distribution(正太分布)
- 关于虚函数的总结
- 设计模式(动态代理)
- 浮动框停留在底部
- swift实现代理
- 优秀jquery幻灯片插件汇总
- eclipse内存溢出报错:java.lang.OutOfMemoryError:Java heap space.
- eclipse内存溢出报错:java.lang.OutOfMemoryError:Java heap space.
- eclipse内存溢出报错:java.lang.OutOfMemoryError:Java heap space.
- 设置动画执行结束之后不移除
- swift 写分类
- LeetCode:Binary Tree Maximum Path Sum
- java调用restful webservice(转)
- android渐变色,边角,边框,
- bzoj 4401: 块的计数
- android拍照与相册选择照片并保存本地
- 【HTML5】表单元素
- 树莓派上实现流媒体