不妨试试transform.localEulerAngles
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,
transform.localEulerAngles.y + 1.0f, transform.localEulerAngles.z);
但我建议您在其中添加Time.deltaTime,否则您的灯将以运行它的计算机的帧速率旋转。因此,如果您想要恒定的速度,请按该值进行修改。
我已编辑以下内容以制作完整的示例。 OP在一个轴上说它在一定程度上停止。我已经扩展它以使用以下代码显示,它将在任何轴和任何方向上工作,在运行时可修改。
using UnityEngine;
public class rotate : MonoBehaviour {
public float speed = 100.0f;
Vector3 angle;
float rotation = 0f;
public enum Axis
{
X,
Y,
Z
}
public Axis axis = Axis.X;
public bool direction = true;
void Start()
{
angle = transform.localEulerAngles;
}
void Update()
{
switch(axis)
{
case Axis.X:
transform.localEulerAngles = new Vector3(Rotation(), angle.y, angle.z);
break;
case Axis.Y:
transform.localEulerAngles = new Vector3(angle.x, Rotation(), angle.z);
break;
case Axis.Z:
transform.localEulerAngles = new Vector3(angle.x, angle.y, Rotation());
break;
}
}
float Rotation()
{
rotation += speed * Time.deltaTime;
if (rotation >= 360f)
rotation -= 360f; // this will keep it to a value of 0 to 359.99...
return direction ? rotation : -rotation;
}
}
然后您可以在运行时修改速度、轴和方向以找到适合您的方法。不过一定要在停止游戏后重新设置,因为它不会被保存。