如何让一个平面只在一个轴上旋转?

How to make a plane to rotate only on one axis?

如何通过仅在一个轴上旋转来使相机"look"成为一个平面?

例如,我有一个飞机,其纹理是从管道中冒出的烟雾。如果我绕着管道走,平面应该始终面向相机,沿 y 轴旋转。但是烟雾的方向不应该改变,因此,平面不应该沿着 x 和 z 轴旋转。

这是一个代码示例,它有助于在所有轴上旋转平面:

void Update() 
{
transform.LookAt(Camera.main.transform.position, -Vector3.up);
}

如何让它只绕一个轴旋转?

一种方法是使用 transform.eulerAngles 将对象的原始旋转存储在 Vector3 中。在 LookAt 函数完成后,您可以创建另一个 Vector3 来存储对象的旋转。然后,您可以仅使用来自第二个变量的 y 值并使用原始 x 和 y 值,将对象的旋转设置为新的 Vector3。它看起来像这样:

void Update()
{
    Vector3 originalRotation = transform.eulerAngles;
    transform.LookAt(Camera.main.transform.position, -Vector3.up);
    Vector3 newRotation = transform.eulerAngles;
    transform.eulerAngles = new Vector3(originalRotation.x, newRotation.y, originalRotation.z);
}