这就是我的问题:我在Unity中有一个对象,那就是我的玩家。我希望这个球员能够面对他移动的方向。但我不知道如何旋转对象,直到释放特定的键,然后将旋转重新定位为0,0,0
我试过这个代码
public class PlayerAction : MonoBehaviour
{
public float horizontalInput;
public float verticalInput;
// Start is called before the first frame update
void Start()
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
if (horizontalInput > 0)
{
transform.Rotate(0, 90, 0);
}
}
}我在看四元数,但在任何论坛上我都不懂任何东西。
为了清楚起见,我想手动更改播放器的旋转。
发布于 2021-02-27 20:46:17
找到这个示例,您可以围绕y轴旋转到每个边,并分别重置回原始旋转。
using UnityEngine;
public class Rotation : MonoBehaviour {
private Quaternion startingRot;
void Start() {
startingRot = transform.rotation;
}
void Update() {
if (Input.GetKeyDown(KeyCode.P)) {
transform.Rotate(0, 45, 0);
}
if (Input.GetKeyDown(KeyCode.O)) {
transform.Rotate(0, -45, 0);
}
if (Input.GetKeyUp(KeyCode.L)) {
transform.rotation = startingRot;
}
}
}四元数是处理旋转的数学工具。它们远不如eulerAngles直观,但在计算上要高效得多,并解决了eulerAngles的一个已知问题,即
万向节lock](https://en.wikipedia.org/wiki/Gimbal_lock)
你暂时不需要担心这一点,只是为了你的信息:
Unity提供了在代码中使用euler角度与旋转进行交互的方法,与编辑器中的方式相同,但旋转是用四元数处理的。这就是为什么你会多次在unity的编辑器变换旋转窗口中发现一些意想不到的值,因为unity会永久地从它内部处理的四元数转换为编辑器用户界面中显示的euler角度。
https://stackoverflow.com/questions/66398842
复制相似问题