c# - 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
}
}
}
解决方案
无论旋转速度如何,您(可能)都不想在一帧的跨度内应用所有旋转。
相反,您应该跟踪在处理该帧队列的过程中您旋转了多少,如果遇到这种情况,请退出 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.
}
}
}
}
}
推荐阅读
- sql - 你能在 SQL Server 的 select 语句中做一个正则表达式吗?
- django - Nginx 无法提供后端的静态文件
- javascript - Electron:在主渲染器和渲染器中使用相同的单例
- python - 从 Django webapp 安全地执行 Go 脚本
- visual-studio-code - 是否有相当于 Notepad++ 的用户定义语言配置的 VSCode?
- assembly - 在汇编语言中进行“添加”和“进位”操作的替代方法是什么?
- size - 如何在 Prometheus 中更改堆大小
- angular - 如何在控制台中访问 ngrx 商店?
- flutter - 在flutter中从firestore获取的应用程序中格式化日期时间
- flutter - 错误:没有名为“onRatingChanged”的命名参数