我正在学习Unity,我有一个问题。
我不明白为什么这条线会将旋转重置为0,0,0。我所做的只是重新分配其euler值?
transform.rotation =
Quaternion.Euler(transform.rotation.x,
transform.rotation.y,
transform.rotation.z);
我这样做的原因是因为我需要锁定一个轴并保持其他轴的变化。所以我认为在x和z轴发生变化之后我可以这样做:
transform.rotation = Quaternion.Euler(transform.rotation.x,
LOCKED ROTATION VALUE,
transform.rotation.z);
我确信这很简单,但我找不到什么错误。
提前致谢。
答案 0 :(得分:2)
Here is some documentation with transform.Rotate()。我用它来旋转。
// Here is an example if you want to just rotate the Z axis.
transform.Rotate(new Vector3(0, 0, 10f) * Time.deltaTime);
Also, here is some other documentation on Quaternion。你可以使用像RotateTowards这样的函数。
如果您的游戏对象上有一个刚体,那么您可以在Unity中锁定它的旋转。单击游戏对象,在Rigidbody组件下单击Constraints,然后单击Freeze Rotation下的轴。
答案 1 :(得分:1)
您正在使用transform.rotation,就好像它正在暴露欧拉角(它不是)。如果你想要旋转的欧拉角,你必须这样做:
transform.rotation =
Quaternion.Euler(transform.rotation.eulerAngles.x,
transform.rotation.eulerAngles.y,
transform.rotation.eulerAngles.z);