拖动三维物体360度旋转3种方法
2016-02-18 14:34
369 查看
//方法一(鼠标左键)
//private Transform hitTransfrom;
//void Update()
//{
// if (Input.GetMouseButtonDown(0))
// {
// RaycastHit hit;
// Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if (Physics.Raycast(mouseray, out hit))
// {
// hitTransfrom = hit.transform;
// }
// }
// else if (Input.GetMouseButtonUp(0))
// {
// hitTransfrom = null;
// }
// if (hitTransfrom)
// {
// Matrix4x4 localmatrix = hitTransfrom.worldToLocalMatrix;//Matrix4x4矩阵 worldToLocalMatrix矩阵坐标世界到自身
// Vector3 vUp = localmatrix.MultiplyVector(new Vector3(0, 1, 0));//MultiplyVector变换方向
// Vector3 vRight = -localmatrix.MultiplyVector(new Vector3(1, 0, 0));
// float fMoveX = -Input.GetAxis("Mouse X") * Time.deltaTime * 200.0f;
// Quaternion rotation = Quaternion.AngleAxis(fMoveX, vUp);//Quaternion.AngleAxis四元数.角轴(float,vector3)
// hitTransfrom.localRotation *= rotation;//localRotation自身旋转角度
// float fMoveY = -Input.GetAxis("Mouse Y") * Time.deltaTime * 200.0f;
// Quaternion rotoy = Quaternion.AngleAxis(fMoveY, vRight);
// hitTransfrom.localRotation *= rotoy;
// }
//}
/// <summary>
/// 方法二(鼠标拖动左键简单)
/// </summary>
//void OnMouseDrag()//OnMouseDrag当鼠标拖动
//{
// this.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), -Input.GetAxis("Mouse X"), 0) * 6f, Space.World);
//}
//方法三(添加了鼠标托拽后的惯性)
private bool onDrag = false;
public float speed = 6f;
private float tempSpeed;
private float axisX;
private float axisY;
void OnMouseDrag()
{
onDrag = true;
axisX = -Input.GetAxis("Mouse X");
axisY = Input.GetAxis("Mouse Y");
}
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;
}
void Update()
{
this.transform.Rotate(new Vector3(axisY, axisX, 0) * Rigid(), Space.World);
if (!Input.GetMouseButton(0))
{
onDrag = false;
}
}
//private Transform hitTransfrom;
//void Update()
//{
// if (Input.GetMouseButtonDown(0))
// {
// RaycastHit hit;
// Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if (Physics.Raycast(mouseray, out hit))
// {
// hitTransfrom = hit.transform;
// }
// }
// else if (Input.GetMouseButtonUp(0))
// {
// hitTransfrom = null;
// }
// if (hitTransfrom)
// {
// Matrix4x4 localmatrix = hitTransfrom.worldToLocalMatrix;//Matrix4x4矩阵 worldToLocalMatrix矩阵坐标世界到自身
// Vector3 vUp = localmatrix.MultiplyVector(new Vector3(0, 1, 0));//MultiplyVector变换方向
// Vector3 vRight = -localmatrix.MultiplyVector(new Vector3(1, 0, 0));
// float fMoveX = -Input.GetAxis("Mouse X") * Time.deltaTime * 200.0f;
// Quaternion rotation = Quaternion.AngleAxis(fMoveX, vUp);//Quaternion.AngleAxis四元数.角轴(float,vector3)
// hitTransfrom.localRotation *= rotation;//localRotation自身旋转角度
// float fMoveY = -Input.GetAxis("Mouse Y") * Time.deltaTime * 200.0f;
// Quaternion rotoy = Quaternion.AngleAxis(fMoveY, vRight);
// hitTransfrom.localRotation *= rotoy;
// }
//}
/// <summary>
/// 方法二(鼠标拖动左键简单)
/// </summary>
//void OnMouseDrag()//OnMouseDrag当鼠标拖动
//{
// this.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), -Input.GetAxis("Mouse X"), 0) * 6f, Space.World);
//}
//方法三(添加了鼠标托拽后的惯性)
private bool onDrag = false;
public float speed = 6f;
private float tempSpeed;
private float axisX;
private float axisY;
void OnMouseDrag()
{
onDrag = true;
axisX = -Input.GetAxis("Mouse X");
axisY = Input.GetAxis("Mouse Y");
}
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;
}
void Update()
{
this.transform.Rotate(new Vector3(axisY, axisX, 0) * Rigid(), Space.World);
if (!Input.GetMouseButton(0))
{
onDrag = false;
}
}
相关文章推荐
- sql server 解锁表
- RootKit Hunter 后端侦测软件之架设与执行
- Codeforces 25C Roads in Berland(floyed)
- JavaScript中的正则表达式简介
- 页面分页插件 Bootstrap Paginator使用方法
- 互联网金融中的各条路子
- easyui dialog 有滚动条时的窗口定位
- js拼接运算符
- [群150178878]nRF51822学习笔记–硬件外设篇【1】
- Button.setOnClickListener(OnClickListener l) 原理
- 【python】编程语言入门经典100例--5
- 冬眠结束,醒醒吧
- Android快速开发 动画系列(一) 之 TranslateAnimation(平移动画)
- EditText(输入框)详解
- 64位win8.1下 安装配置instant client & plsqldev
- 理解Docker容器
- 错误: 程序包com.sun.istack.internal不存在
- iOS开发--AVFoundation自定义相机
- 我的PHP之路
- Fortran 第二课