【发布时间】:2013-12-02 13:26:58
【问题描述】:
我正在使用 Unity3D。我想旋转一个对象以面向鼠标指针的方向,但允许最大旋转速度,例如“每秒最大 100 度”。
文档中有一个示例,但它没有做我想要的。
我认为Time.time应该是Time.deltaTime,我真的无法理解最后一个参数的作用。它应该是与起始向量相加的数字吗?
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html
另外,我无法真正理解最后一个参数的作用。轮换时间到了吗?
我现在使用的代码
Plane plane = new Plane(Vector3.up, 0);
float dist;
void Update () {
//cast ray from camera to plane (plane is at ground level, but infinite in space)
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out dist)) {
Vector3 point = ray.GetPoint(dist);
//find the vector pointing from our position to the target
Vector3 direction = (point - transform.position).normalized;
//create the rotation we need to be in to look at the target
Quaternion lookRotation = Quaternion.LookRotation(direction);
//rotate towards a direction, but not immediately (rotate a little every frame)
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
}
}
我认为弱点在 Slerp 的第三个参数中,但我不知道该放什么。
【问题讨论】:
-
参数为:开始旋转,结束旋转,到目前为止已经完成的旋转的分数。当使用
Time.deltaTime并使用当前变换而不是第一次开始旋转时的变换时,那么第三个参数基本上就是这一帧应该旋转多少。您现在的代码是否有效? -
不,它几乎是立即旋转。
标签: c# rotation unity3d quaternions linear-interpolation