拖动模型任意方向旋转

来源:互联网 发布:python常数的矩阵次方 编辑:程序博客网 时间:2024/04/29 04:20

今天新的任务需要修改一下模型的旋转,以前的旋转方式是左右拖动屏幕可以使得模型绕着自身Y轴旋转,现在改为任意触摸屏幕旋转,跟着触摸移动的方向进行旋转,这里仅仅为记录一下代码,方便以后查找,因为博主比较懒,刚开始得到这个需求的时候也百度谷歌了一番,居然没有找到这个效果,于是只好自己动手丰衣足食。

核心代码为:

//当前触摸的坐标与上一个触摸坐标的偏移量Vector2 offsetPos = m_TouchCurPos - m_TouchPrePos;//相机到目标的向量  Vector3 camRelativeToTarget = Vector3.Normalize(Cam.transform.position - transform.position);//触摸为照相机坐标,将此坐标转为世界坐标  Vector3 touchCurWorldPos = Cam.transform.localToWorldMatrix.MultiplyPoint(new Vector3(m_TouchCurPos.x, m_TouchCurPos.y, 0f));  Vector3 touchPreWorldPos  = Cam.transform.localToWorldMatrix.MultiplyPoint(new Vector3(m_TouchPrePos.x, m_TouchPrePos.y, 0f));  //触摸方向向量转世界向量  Vector3 touchVec = touchCurWorldPos - touchPreWorldPos;  //旋转轴为camRelativeToTarget 与touchVec 两向量形成的平面的法向量  Vector3 roundAxis = Vector3.Cross(touchVec, camRelativeToTarget);  transform.Rotate (roundAxis, Space.World);


0 0
原创粉丝点击