C# 如何围绕对象自身旋转?

C# 如何围绕对象自身旋转?,c#,unity3d,rotation,quaternions,C#,Unity3d,Rotation,Quaternions,我想在Unity3D中旋转一个立方体。当我按下键盘上的向左箭头按钮时,立方体必须向左旋转。如果我向上推,立方体必须向上旋转。但在我的脚本中,立方体向左旋转,然后左侧向上旋转 这是当前状态: 这就是我想要的: 您需要交换四元数乘法的顺序。 按照现在的方式,旋转将应用于轴,就像它们在原始旋转之后一样,因为您正在有效地执行targetRotation=targetRotation* 但是,您希望绕世界轴旋转旋转。您可以通过执行targetRotation=…*目标定位: void Update()

我想在Unity3D中旋转一个立方体。当我按下键盘上的向左箭头按钮时,立方体必须向左旋转。如果我向上推,立方体必须向上旋转。但在我的脚本中,立方体向左旋转,然后左侧向上旋转

这是当前状态:

这就是我想要的:

您需要交换四元数乘法的顺序。 按照现在的方式,旋转将应用于轴,就像它们在原始旋转之后一样,因为您正在有效地执行
targetRotation=targetRotation*

但是,您希望绕世界轴旋转旋转。您可以通过执行
targetRotation=…*目标定位

void Update()
{
    if(Input.GetKeyDown(KeyCode.UpArrow)){
        targetRotation = Quaternion.AngleAxis(90, Vector3.right) * targetRotation;
    }
    if(Input.GetKeyDown(KeyCode.DownArrow)){
        targetRotation = Quaternion.AngleAxis(90, Vector3.left) * targetRotation;
    }
    if(Input.GetKeyDown(KeyCode.LeftArrow)){
        targetRotation = Quaternion.AngleAxis(90, Vector3.up) * targetRotation;
    }
    if(Input.GetKeyDown(KeyCode.RightArrow)){
        targetRotation = Quaternion.AngleAxis(90, Vector3.down) * targetRotation;
    }
    transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 10* smooth * Time.deltaTime);

}
有关更多信息,请参阅

void Update()
{
    if(Input.GetKeyDown(KeyCode.UpArrow)){
        targetRotation = Quaternion.AngleAxis(90, Vector3.right) * targetRotation;
    }
    if(Input.GetKeyDown(KeyCode.DownArrow)){
        targetRotation = Quaternion.AngleAxis(90, Vector3.left) * targetRotation;
    }
    if(Input.GetKeyDown(KeyCode.LeftArrow)){
        targetRotation = Quaternion.AngleAxis(90, Vector3.up) * targetRotation;
    }
    if(Input.GetKeyDown(KeyCode.RightArrow)){
        targetRotation = Quaternion.AngleAxis(90, Vector3.down) * targetRotation;
    }
    transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 10* smooth * Time.deltaTime);

}