I am trying to make my directional light rotate at a constant speed. This is the code that I have:
using System.Collections;
using UnityEngine;
public class LightRotator : MonoBehaviour {
void Update () {
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + 1.0f, transform.rotation.z);
}
}
However, this simply puts the light in a weird spot and leaves it there. What am I doing wrong?
This is the rotation of the light before I run the game (should be the start position):
Maybe try transform.localEulerAngles
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,
transform.localEulerAngles.y + 1.0f, transform.localEulerAngles.z);
But I suggested you add Time.deltaTime
to that, or your light will spin at the frame rate of the computer running it. So if you want a constant speed, modify it by that value.
I've edited the following to make a complete example. The OP was saying on one axis it stops at a certain degree. I've expanded this to show with the following code, it will work on whatever axis and in whatever direction, modifiable at runtime.
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;
}
}
Then you can modify speed, axis and direction at runtime to find what works for you. Though be sure to set it again after you stop the game, as it won't be saved.