首页 > 解决方案 > Unity3d 根据 Pitch、Roll 和 Yaw 角度列表平滑旋转对象

问题描述

我想根据从 API 调用中获得的角度列表(俯仰、滚动和偏航)平滑旋转对象(平面)。响应对象是下面的Rootresponse

public class ResponseData
{
    public List<int> x; //roll
    public List<int> y; //yaw
    public List<int> z; //pitch
}

public class RootResponse
{
    public ResponseData data;
    public string status; //status of the api call
}

我尝试使用下面的一段代码使用 while 循环遍历 FixedUpdate方法中每一帧的值。这会引发“ArgumentOutOfRange”异常。

如果我按照文档使用 transform.Roatate 或 Quarternion 角度,我只能获得最终位置。

在这种情况下,我可以选择的最佳方法是什么?

void FixedUpdate(){
    if(shouldUpdate) { //set to true for a success response from the api call
        while(ind < dataLen) {
            transform.position = Vector3.MoveTowards(new Vector3(batData.x[ind], batData.y[ind], batData.z[ind]), new Vector3(batData.x[ind+1], batData.y[ind + 1], batData.z[ind + 1]), speed * Time.deltaTime);
            ind++; //to increment the index every frame
        }
    }
}

标签: c#unity3d

解决方案


无论旋转速度如何,您(可能)都不想在一帧的跨度内应用所有旋转。

相反,您应该跟踪在处理该帧队列的过程中您旋转了多少,如果遇到这种情况,请退出 while 循环。

public float maxAngleRotatePerFrame = 5f;

void FixedUpdate(){
    if(shouldUpdate) { //set to true for a success response from the api call
        // Keep track of how far we've traveled in this frame.
        float angleTraveledThisFrame = 0f;

        // Rotate until we've rotated as much as we can in a single frame, 
        // or we run out of rotations to do.
        while (angleTraveledThisFrame < maxAngleRotatePerFrame && ind < dataLen) {
            // Figure out how we want to rotate and how much that is
            Quaternion curRot = transform.rotation;
            Quaternion goalRot = Quaternion.Euler(
                    batData.x[ind],
                    batData.y[ind],
                    batData.z[ind]
                    );
            float angleLeftInThisInd =  Quaternion.Angle(curRot, goalRot);

            // Rotate as much as we can toward that rotation this frame
            float curAngleRotate = Mathf.Min(
                    angleLeftInThisInd, 
                    maxAngleRotatePerFrame - angleTraveledThisFrame
                    );

            transform.rotation = Quaternion.RotateTowards(curRot, goalRot, curAngleRotate);

            // Update how much we've rotated already. This determines
            // if we get to go through the while loop again.
            angleTraveledThisFrame += curAngleRotate;

            if (angleTraveledThisFrame < maxAngleRotatePerFrame ) {  
                // If we have more rotating to do this frame,
                // increment the index.
                ind++;

                if (ind==dataLen) {
                    // If you need to do anything when you run out of rotations, 
                    // you can do it here.
                }
            }
        }
    }
}

推荐阅读