C# Unity c->将骨骼旋转到目标向量3
我想操纵骨骼,这样它可以在操纵完成后回到初始旋转,这是我的代码C# Unity c->将骨骼旋转到目标向量3,c#,unity3d,transform,C#,Unity3d,Transform,我想操纵骨骼,这样它可以在操纵完成后回到初始旋转,这是我的代码 void toInitialRotation(Transform theBone, Quaternion theInitialRotation) { //bone.rotation = initialRotation; //this work but move instantly not animated var step = 20 * Time.deltaTime; // The step size i
void toInitialRotation(Transform theBone, Quaternion theInitialRotation)
{
//bone.rotation = initialRotation; //this work but move instantly not animated
var step = 20 * Time.deltaTime; // The step size is equal to speed times frame time.
// Rotate our transform a step closer to the target's.
theBone.rotation = Quaternion.RotateTowards(bone.rotation, theInitialRotation, step); //it's work and animated well
}
上面的代码工作得很好,动画效果也很好,但它是基于步长工作的,我的问题是如何使它基于时间工作,例如,如果给定的时间是3秒,那么骨骼。旋转必须在3秒达到初始旋转,并在那里停止。
感谢您的帮助您可以使用和类似的
IEnumerator toInitialRotation(Transform theBone, Quaternion targetRotation, float duration)
{
// If there is no duration immediately jump to the target rotation and terminate the routine
if(time <= 0)
{
theBone.rotation = targetRotation;
yield break;
}
var currentRotation = theBone.rotation;
var timePassed = 0f;
while(timePassed < duration)
{
// Will move from 0 to 1
var factor = timePassed / duration;
// Interpolates from the current to the target rotation
theBone.rotation = Quaternion.Slerp(currentRotation, targetRotation, factor);
// Increase by the time passed since last frame
timePassed += Time.deltaTime;
// Tells Unity to "pause" the routine, render this frame
// and continue from here in the next frame
yield return null;
}
// Just to be sure set the rotation fix when done
theBone.rotation = targetRotation;
}
如果帮助你,请考虑给他们一些信誉点,以及帮助未来的访问者用类似的问题找到它。
StartCoroutine(toInitialRotation(bone, initialRotation, 3f);