[英]Unity3D Slerp rotation speed
I'm using Unity3D. 我正在使用Unity3D。 I'd like to rotate an object to face the direction of the mouse pointer, but allow a maximum rotation speed, like "max 100 degrees per second". 我想旋转一个物体面向鼠标指针的方向,但允许最大旋转速度,如“每秒最大100度”。
There is an example in the doc, but it does not do what I want. 文档中有一个例子,但它没有做我想要的。
I think the Time.time should be Time.deltaTime, and I can't really understand what the last parameter does. 我认为Time.time应该是Time.deltaTime,我无法真正理解最后一个参数的作用。 Is it supposed to be the number that gets summed to the start vector? 它应该是与起始向量相加的数字吗?
http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html
Also, I can't really understand what the last parameter does. 另外,我无法真正理解最后一个参数的作用。 Is it a time for the rotation? 这是轮换的时候吗?
The code I'm using now 我正在使用的代码
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);
}
}
I think the weak spot is in the third parameter of the Slerp, but I can't figure out what to put there. 我认为弱点位于Slerp的第三个参数中,但我无法弄清楚要放在那里的内容。
You need to maintain a separate variable for the interpolation and update that every frame. 您需要为插值维护一个单独的变量并更新每一帧。 Otherwise your Time.deltaTime * rotationSpeed will keep going up forever past the 0-1 range. 否则,你的Time.deltaTime * rotationSpeed会一直持续超过0-1范围。
private float _RawLerp;
private float _Lerp;
public float _Speed;
public transform _Source;
public transform _Target;
private transform _TransformCache; // the transform for my game object, set in the Awake method
public void Update()
{
_RawLerp += Time.deltaTime * _Speed;
_Lerp = Mathf.Min(_RawLerp, 1);
_TransformCache.rotation = Quaternion.Slerp(
_Source.TargetRotation(),
_Target.TargetRotation(),
_Lerp);
}
This code works, but I'm not sure it's 100% correct. 这段代码有效,但我不确定它是100%正确的。
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);
float angle = Quaternion.Angle(transform.rotation, lookRotation);
float timeToComplete = angle / rotationSpeed;
float donePercentage = Mathf.Min(1F, Time.deltaTime / timeToComplete);
//rotate towards a direction, but not immediately (rotate a little every frame)
//The 3rd parameter is a number between 0 and 1, where 0 is the start rotation and 1 is the end rotation
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, donePercentage);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.