1.视频效果:
工程百度网盘链接:
链接:https://pan.baidu.com/s/1OYkt2T3Wv_Hh0Bt7nLyR-A
提取码:1212
2.设计思路:
我们从鼠标点击的屏幕坐标打出一根射线,求出射线和旋转面的交点,交点减去原点,求出向量,再用Vector3.SignedAngle求出与第一次点击时初始向量的大小角度对比,最终获得当前帧的旋转分量。
3.关键代码:
public void CalculateClickPos(){//屏幕坐标转世界curFrameRay = Camera.main.ScreenPointToRay(Input.mousePosition);curFramePos = CalculateRayPlaneIntersection(curFrameRay.origin, curFrameRay.origin-Camera.main.transform.position, transform.up, transform.position);dragVec = curFramePos - transform.position;curFrameAngle = Vector3.SignedAngle(transform.forward, dragVec,transform.up);}/// <summary>/// 求射线和平面的交点/// </summary>/// <param name="rayOrigin"></param>/// <param name="rayDirection"></param>/// <param name="planeNormal"></param>/// <param name="pointOnPlane"></param>/// <returns></returns>public Vector3 CalculateRayPlaneIntersection(Vector3 rayOrigin, Vector3 rayDirection, Vector3 planeNormal, Vector3 pointOnPlane){float dot = Vector3.Dot(planeNormal, rayDirection);// 确保射线不平行于平面if (Mathf.Abs(dot) > 0.0001f){// 计算射线和平面的交点参数float t = Vector3.Dot(pointOnPlane - rayOrigin, planeNormal) / dot;// 计算交点Vector3 intersectionPoint = rayOrigin + t * rayDirection;return intersectionPoint;}// 如果射线平行于平面,返回 Vector3.zero 表示没有交点return Vector3.zero;}