- 最后登录
- 2019-12-25
- 注册时间
- 2012-8-24
- 阅读权限
- 90
- 积分
- 71088
- 纳金币
- 52328
- 精华
- 343
|
控制球杆绕奈转动,首先能想到的恐怕是RotateAround吧,用这个方法先得计算出偏移角度、转动方向,然后再插值旋转。今天发现的新方法是利用Plane和射线做旋转:首先在目标物体的位置初始一个Plane,再用屏幕射线获得与Plane的交点,用交点和目标位置所得的向量,确定旋转。
代码记录:
- void Start()
- {
- mPlane = new Plane(Vector3.up, transform.position);
- }
复制代码- void Update()
- {
- if (Input.GetMouseButton(0))
- {
- Vector3 mousePos = Input.mousePosition;
- Vector3 targetPos = InterSectionPoint(mPlane, mousePos, Camera.main);
- Vector3 dirction = transform.position - targetPos;
- float dist = dirction.magnitude;
- if (dirction != Vector3.zero)
- {
- arrow.rotation = Quaternion.LookRotation(dirction);
- }
- else
- {
- arrow.LookAt(transform);
- }}
- }
复制代码
- /// <summary>
- /// 获得射线与平面交点
- /// </summary>
- /// <param name="plane"></param>
- /// <param name="screenPoint"></param>
- /// <param name="cam"></param>
- /// <returns></returns>
- Vector3 InterSectionPoint(Plane plane, Vector3 screenPoint,Camera cam)
- {
- Vector3 point = Vector3.zero;
- float dist;
- Ray ray = cam.ScreenPointToRay(screenPoint);
- plane.Raycast(ray, out dist);
- return ray.GetPoint(dist);
- }
复制代码
|
|