0

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): Start Position

Once the game starts, it changes to (and stays at) this: Wrong Position

Johnny Dollard
  • 708
  • 3
  • 11
  • 26

2 Answers2

1

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.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
  • When I change the Y and Z axis rotations they work fine and continue to spin, but the rotation on the X axis stops at 90º, and -90º. Any ideas? – Johnny Dollard Mar 17 '17 at 22:59
  • Not without seeing more code. I suggest accepting this answer, since it does rotate as I set forth. Post a new question. – Chuck Savage Mar 18 '17 at 01:11
  • Had a chance to test this. Made it more complete, but the functionality remains the same. It works on all 3 axis, no matter the direction. – Chuck Savage Mar 18 '17 at 05:07
  • I'm still fairly new to C# and don't really know what values to adjust in order to make it move. Could you point me in the right direction please? – Johnny Dollard Mar 18 '17 at 16:19
  • in the inspector, where you have the c# script added as a component to the light. There will be 3 values, one a speed number, an axis combo box x, y or z, and direction a check box. These can be changed when the game is playing, to see the effects. It'll be easier to see, if you add the script to a cube, you'll see it rotates just fine in any direction. – Chuck Savage Mar 19 '17 at 19:12
0

You are missing the W component from the rotation, and that's causing the problem in your code. Try this:

transform.rotation = Quaternion.Euler(transform.eulerAngles.x, 
                                      transform.eulerAngles.y + 1.0f,
                                      transform.eulerAngles.z);

Also, take a look at these here:

http://answers.unity3d.com/questions/123827/transformrotate-stuck-at-90-and-270-degrees.html http://answers.unity3d.com/questions/187073/rotation-locks-at-90-or-270-degrees.html

I recommend the second one.

Johnny Dollard
  • 708
  • 3
  • 11
  • 26
pasotee
  • 338
  • 2
  • 12
  • Like the other answer, this works fine except that rotation on the X axis stops at 90º, and -90º. Do you know how to fix this? – Johnny Dollard Mar 18 '17 at 00:56
  • Yeah, I did some research, look here: http://answers.unity3d.com/questions/123827/transformrotate-stuck-at-90-and-270-degrees.html and this one: http://answers.unity3d.com/questions/187073/rotation-locks-at-90-or-270-degrees.html I recomand the second one – pasotee Mar 18 '17 at 06:13
  • What exactly is the W component? – Johnny Dollard Mar 18 '17 at 16:25