简体   繁体   English

在X和Z GLOBAL轴上旋转对象

[英]Rotate an Object on X and Z GLOBAL axis

i have the following scenario: 我有以下情况:

I have a cylinder in my scene and I hace two Dropdowns. 我的场景中有一个圆柱,并且有两个Dropdowns。 With the Dropdowns, I can select a rotation on X and Z axis of either 0 or 90 degrees. 通过下拉菜单,我可以选择X轴和Z轴的旋转角度为0或90度。 This rotation always has to be on the global axis so the user understands whats happening. 此旋转始终必须在全局轴上,以便用户了解正在发生的事情。

When i rotate the object 90deg on the X axis, everything is ok. 当我在X轴上将对象旋转90度时,一切正常。 When i try to rotate it by 90deg on the Z axis aditionally, the object is being rotated around the Y axis, not the Z axis. 当我尝试将其在Z轴上附加旋转90度时,对象正在绕Y轴而不是Z轴旋转。

I have tried to do with with setting transform.rotation directly, transform.Rotate and transform.RotateAround but nothing worked. 我试图直接设置transform.rotation,transform.Rotate和transform.RotateAround,但是没有任何效果。

The first thing I do is always resetting the rotation to 0. 我要做的第一件事就是始终将旋转度重置为0。

Object.transform.rotation = Quaternion.identity;

My Try with Eulers: 我对Eulers的尝试:

Object.transform.rotation = Quaternion.Euler(new Vector3(xRot, 0, zRot));

My Try with Quaternions: 我的四元数尝试:

Object.transform.rotation = Quaternion.AngleAxis(xRot * 90, Vector3.right) * Quaternion.AngleAxis(zRot * 90, Vector3.forward);

My Try with Rotate: 我的旋转尝试:

Object.transform.Rotate(new Vector3(xRot, 0, zRot), Space.World);

My expected result is the object to always rotate around the GLOBAL X/Z axis, no matter what the objects current rotation might be. 我的预期结果是,无论当前旋转的对象是什么,该对象始终绕GLOBAL X / Z轴旋转。 But none of my tries resulted in the expected behaviour. 但是我的尝试都没有导致预期的行为。

You can use this: 您可以使用此:

gameObject.transform.localEulerAngles = new Vector3(xRot, 0, zRot);

Or this for global axis: 或对于全局轴:

gameObject.transform.eulerAngles = new Vector3(xRot, 0, zRot);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM