using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightRotate : MonoBehaviour {
private int rotatespeed = 25;
private float xRotate;
private float yRotate;
public Light DirectionLight;
void Update ()
{
float xRotation = DirectionLight.transform.eulerAngles.x;
float yRotation = DirectionLight.transform.eulerAngles.y;
DirectionLight.transform.eulerAngles = new Vector3(xRotation + Time.deltaTime * rotatespeed, yRotation + Time.deltaTime * rotatespeed , 0);
xRotate = xRotation + Time.deltaTime * rotatespeed;
yRotate = yRotation + Time.deltaTime * rotatespeed;
Debug.Log (xRotate + " + " + yRotate);
}
}
So, I am trying to emulate sunrise and sunset using this code above, but for some reason, I may have screwed up the Vector3 equations, as the directional light would end up facing downwards and be stuck in an infinite loop.
Furthermore, I am unable to have the Debug show up in console with regards to the xRotate and yRotate value.
Question: Will I be able to get the xRotate values in this manner? I want to create a “for” loop to enable lights components in other GameObjects in the scene being turned on during night time (dependent on the xRotate value).
Much help will be appreciated.
Thank you.
It’s best to keep your own values for xRotation and yRotation and not take them from the transform each time. This is because the same rotation can be represented by different euler angles, especially at points when the axis is pointing downwards or upwards.
For instance, an object at rotation (90,90,0) would look the same as one at (90,180,90). When you set an euler value in Unity it might change it internally to a different representation and then when you read it back you’ll end up stuck in a loop.
If you just change your code to put this code in the Awake or Start function it should work: (and take it out of the Update loop)
void Awake()
{
float xRotation = DirectionLight.transform.eulerAngles.x;
float yRotation = DirectionLight.transform.eulerAngles.y;
}
Hi, I have just amended the code, putting the xRotation and yRotation as private variables, and moved both into the void Awake function. Now the light is stuck in an infinite loop aiming upwards.
I’m sorry, but I am new to this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightRotate : MonoBehaviour {
private int rotatespeed = 25;
private float xRotate;
private float yRotate;
private float xRotation;
private float yRotation;
public Light DirectionLight;
void Awake ()
{
float xRotation = DirectionLight.transform.eulerAngles.x;
float yRotation = DirectionLight.transform.eulerAngles.y;
}
void Update ()
{
DirectionLight.transform.eulerAngles = new Vector3(xRotation + Time.deltaTime * rotatespeed, yRotation + Time.deltaTime * rotatespeed , 0);
xRotate = Time.deltaTime * rotatespeed;
yRotate = Time.deltaTime * rotatespeed;
Debug.Log (xRotate + " + " + yRotate);
}
}
Your code is nearly right but you don’t need the xRotate as well as xRotation - you still need to add time rotateSpeed to the xRotation as well. Something like this should work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightRotate : MonoBehaviour {
private int rotatespeed = 25;
private float xRotation;
private float yRotation;
public Light DirectionLight;
void Awake ()
{
float xRotation = DirectionLight.transform.eulerAngles.x;
float yRotation = DirectionLight.transform.eulerAngles.y;
}
void Update ()
{
DirectionLight.transform.eulerAngles = new Vector3(xRotation, yRotation, 0);
xRotation += Time.deltaTime * rotatespeed;
yRotation += Time.deltaTime * rotatespeed;
Debug.Log (xRotation + " + " + yRotation);
}
}
Thank you so much! It is working now!