0

I have tried both of these C#scripts to rotate my directional light:

using System.Collections;
using UnityEngine;

public class LightRotator : MonoBehaviour
{

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

and

using System.Collections;
using UnityEngine;

public class LightRotator : MonoBehaviour
{

    void Update ()
    {    
        transform.localEulerAngles = new Vector3(transform.localEulerAngles.x + 1.0f, 
                                                 transform.localEulerAngles.y, 
                                                 transform.localEulerAngles.z);
    }
}

They both seem to function exactly the same: If I change transform.eulerAngles.y to transform.eulerAngles.y + 0.5f, the light will rotate along the y-axis, and the same works for the z-axis. However, when I try to do this with the x-axis, it will rotate until it hits 90º, at which point it will continue to attempt rotation but it immediately and continuously shoved back to 90º. If I reverse the direction, it does the same thing at -90º. For example, the rotation might be: 88.5,89.0,89.5,90.0, 90.5, 89.93, 90.24, 89.4, etc.

What is causing this clamping and how do I fix it?

Johnny Dollard
  • 708
  • 3
  • 11
  • 26

4 Answers4

0

I think this is what you are looking for: http://answers.unity3d.com/questions/187073/rotation-locks-at-90-or-270-degrees.html

pasotee
  • 338
  • 2
  • 12
  • That seems to be my problem, but does not tell me how to fix it. Also, I tried transform,rotate and it wouldn't rotate at all in any direction. I believe transform only works for position and scale. – Johnny Dollard Mar 18 '17 at 16:34
  • https://docs.unity3d.com/ScriptReference/Quaternion.html To be more exact this: https://docs.unity3d.com/ScriptReference/Quaternion-w.html – pasotee Mar 18 '17 at 17:30
0

In order to fix your problem, you need to use an additional vector, change it inside Update every frame, and then pass it to the eulerAngles propriety of the transform.

Vector3 vect = Vector3.zero;
float rotationSpeed = 10f;

void Start () {
    vect = transform.eulerAngles; //Set the vect rotation equal to the game object's one
}

void Update ()
{
    vect.x += rotationSpeed * Time.deltaTime;

    //Pass unique eulerAngles representation to the object without letting Unity change it
    transform.eulerAngles = vect;
}

This happens btw because there're multiple euler angles representation of a single physical rotation in the 3D space, and when you work directly on the eulerAngles propriety of the transform, Unity makes some work behind the scenes, which can lead to a gimbal lock.

Galandil
  • 4,169
  • 1
  • 13
  • 24
0

Use Quaternions. It's what Unity3d uses internally and doesn't have any of the side effects of euler angles.

using System.Collections;
using UnityEngine;

public class LightRotator : MonoBehaviour
{
    public Vector3 RotationAxis = Vector3.right;

    Quaternion _startRotation;
    float _rotationIncrement = 0;
    void Start()
    {
         _startRotation = transform.rotation;
    }
    void Update ()
    {
        Quaternion rotationMod = 
            Quaternion.AngleAxis(_rotationIncrement, RotationAxis);
        _rotationIncrement += 1;
        transform.rotation = _startRotation * rotationMod;
    }
}

However, you probably want to use something like Quaternion.RotateTowards or Quaternion.Lerp along with Time.time and a rate. You will get much smoother results that way.

avariant
  • 2,234
  • 5
  • 25
  • 33
0

if you only want to rotate along the X-axis then set the other axis as 0.