【发布时间】:2015-04-28 03:27:09
【问题描述】:
我有一个方法可以将我的对象旋转到鼠标刚刚点击的位置。但是,只要按住鼠标按钮,我的对象就会旋转。
我的主要轮换方法是这样的:
void RotateShip ()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Debug.Log (Input.mousePosition.ToString ());
Plane playerPlane = new Plane (Vector3.up, transform.position);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint (hitdist);
Quaternion targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
我在 FixedUpdate 方法中调用此方法,并将其包装在以下 if 语句中:
void FixedUpdate ()
{
// Generate a plane that intersects the transform's position with an upwards normal.
// Generate a ray from the cursor position
if (Input.GetMouseButton (0))
{
RotateShip ();
}
}
但是,只要按住鼠标按钮,对象仍然会旋转。我希望我的对象继续旋转到鼠标刚刚单击的点,直到它到达该点。
如何正确修改我的代码?
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。