纳金网
标题:
unity中关于鼠标控制问题(比如控制球杆绕奈转动)
[打印本页]
作者:
may
时间:
2014-8-31 16:34
标题:
unity中关于鼠标控制问题(比如控制球杆绕奈转动)
控制球杆绕奈转动,首先能想到的恐怕是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);
}
复制代码
欢迎光临 纳金网 (http://c-www.narkii.com/club/)
Powered by Discuz! X2.5