也許嘗試transform.localEulerAngles
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,
transform.localEulerAngles.y + 1.0f, transform.localEulerAngles.z);
但我建議你添加Time.deltaTime
到,或者你的光將在共同的幀速率旋轉mputer運行它。所以如果你想要一個恆定的速度,修改它的值。
我編輯了以下內容來製作完整的示例。 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;
}
}
然後,您可以在運行時修改速度,軸和方向以找到適合您的方法。儘管一定要在停止遊戲後重新設置,因爲它不會被保存。
當我改變Y軸和Z軸旋轉時,它們工作正常並繼續旋轉,但X軸上的旋轉停止在90º和-90º。有任何想法嗎? – TrumpetDude
不是沒有看到更多的代碼。我建議接受這個答案,因爲它的確如我所闡述的那樣旋轉。發佈一個新問題。 –
有機會測試這個。使它更完整,但功能保持不變。它適用於所有3軸,無論方向如何。 –