Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity c->将骨骼旋转到目标向量3_C#_Unity3d_Transform - Fatal编程技术网

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);