使用 eulerAngles 的对象旋转无法正常工作
object rotation with eulerAngles not working properly
我正在尝试根据箭头键旋转对象,但它卡住了。左右键工作正常,但上下键卡在 90 度。这是我的代码:
void turnCube()
{
if(Input.GetKey (KeyCode.LeftArrow))
{
Camera.main.transform.parent.transform.eulerAngles += new Vector3(0,1,0);
}
if(Input.GetKey (KeyCode.RightArrow))
{
Camera.main.transform.parent.transform.eulerAngles += new Vector3(0,-1,0);
}
if(Input.GetKey (KeyCode.UpArrow))
{
Camera.main.transform.parent.transform.eulerAngles += new Vector3(1,0,0);
}
if(Input.GetKey (KeyCode.DownArrow))
{
Camera.main.transform.parent.transform.eulerAngles += new Vector3(-1,0,0);
}
}
来自 Unity3D API,
Transform.eulerAngles
Only use this variable to read and set the angles to absolute values.
Don't increment them, as it will fail when the angle exceeds 360
degrees. Use Transform.Rotate instead.
所以使用Transform.Rotate,
if(Input.GetKey (KeyCode.LeftArrow))
{
Camera.main.transform.Rotate(Vector3.left * Time.deltaTime);
}
我正在尝试根据箭头键旋转对象,但它卡住了。左右键工作正常,但上下键卡在 90 度。这是我的代码:
void turnCube()
{
if(Input.GetKey (KeyCode.LeftArrow))
{
Camera.main.transform.parent.transform.eulerAngles += new Vector3(0,1,0);
}
if(Input.GetKey (KeyCode.RightArrow))
{
Camera.main.transform.parent.transform.eulerAngles += new Vector3(0,-1,0);
}
if(Input.GetKey (KeyCode.UpArrow))
{
Camera.main.transform.parent.transform.eulerAngles += new Vector3(1,0,0);
}
if(Input.GetKey (KeyCode.DownArrow))
{
Camera.main.transform.parent.transform.eulerAngles += new Vector3(-1,0,0);
}
}
来自 Unity3D API,
Transform.eulerAngles
Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees. Use Transform.Rotate instead.
所以使用Transform.Rotate,
if(Input.GetKey (KeyCode.LeftArrow))
{
Camera.main.transform.Rotate(Vector3.left * Time.deltaTime);
}