平滑的角色旋转
这是我在四个基本方向上移动的代码:
if (Input.GetKeyDown(KeyCode.RightArrow)){
transform.forward = new Vector3(0f, 0f, 1f);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow)){
transform.forward = new Vector3(0f, 0f, -1f);
}
else if (Input.GetKeyDown(KeyCode.DownArrow)){
transform.forward = new Vector3(1f, 0f, 0f);
}
else if (Input.GetKeyDown(KeyCode.UpArrow)){
transform.forward = new Vector3(-1f, 0f, 0f);
}
我的角色立即在四个行进方向上旋转 我遇到的问题是试图弄清楚如何在方向之间平滑。我想说的是,如果角色指向北,并且您按下按钮向西走,我希望看到角色向西方向平滑地旋转一定角度,而不是立即转向西。
我搜索并尝试了教程,但没有一个起作用。有什么解决办法吗?
Here is my code for moving in the four cardinal directions:
if (Input.GetKeyDown(KeyCode.RightArrow)){
transform.forward = new Vector3(0f, 0f, 1f);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow)){
transform.forward = new Vector3(0f, 0f, -1f);
}
else if (Input.GetKeyDown(KeyCode.DownArrow)){
transform.forward = new Vector3(1f, 0f, 0f);
}
else if (Input.GetKeyDown(KeyCode.UpArrow)){
transform.forward = new Vector3(-1f, 0f, 0f);
}
My character instantly rotates in the four directions of travel The problem I'm having is trying to figure out how I can smooth out inbetween directions. What I'm trying to say is, if the character is pointing north and you push the button to go west, instead of instantly turning west, I would like to see the character rotate smoothly in degrees to the westward direction.
Ive search and tried tutorials, but none of them worked. Any solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 Slerp 函数。这可能会做你想要的。
http://unity3d.com/support/documentation/ScriptReference/Quaternion.Slerp.html
简单的例子:
所以你基本上得到了当前旋转的四元数。将其复制到新的四元数中,并增加或减少一个或多个轴上的旋转。
Take a look at the Slerp function. This will probably do what you want.
http://unity3d.com/support/documentation/ScriptReference/Quaternion.Slerp.html
Simple example:
So you basically get the Quaternion of your current rotation. Copy that in a new Quaternion and increase or decrease the the rotation on one or more axis.