Rotating directional light

Im trying to make a daylight system that works off of a clock in my hud, so whenever i change the “game time” the sun/moonlight will react accordingly. I plan on having multiple lights set up in my scene as part of the rig, so i’ve decided to use a military time structure, and say time=2300, then i can make sure street lights are on, the main sun’s rotating in the right direction, etc. i believe i’m on the right track, but I am pretty new at coding, so here’s what i’ve come up with (be gentle :smile:)

//====================================//
//
//			  Daylight System
//			(c)2010 Bill Hayes
//
//====================================//

// Variable Declarations ===============//

var sun : Light;
var timeofday : float;
var azimuth : int;
var elevation : int;
var brightscale : float;

//Function Declarations =================//

function IncrementTime()
{
timeofday+=99;
}

//=====================================//

function Update ()
{

//=====================================//
if (Input.GetKeyUp("e"))
{
IncrementTime();
}

//=======================================//
var lightRotation;
switch(timeofday)
{
case 0:
	lightRotation = Vector3 (0, 0, 0);
	sun.transform.Rotate (lightRotation);
	//timeofday++;
	break;
case 100:
	sun.transform.Rotate (1,0,0);
	break;
case 200:
	sun.transform.Rotate (1,0,0);
	break;
case 300:
	sun.transform.Rotate (1,0,0);
	break;
case 400:
	sun.transform.Rotate (1,0,0);
	break;
case 500:
	sun.transform.Rotate (1,0,0);
	break;
case 600:
	sun.transform.Rotate (1,0,0);
	break;
case 700:
	sun.transform.Rotate (1,0,0);
	break;
case 800:
	sun.transform.Rotate (1,0,0);
	break;
case 900:
	sun.transform.Rotate (1,0,0);
	break;
case 1000:
	sun.transform.Rotate (1,0,0);
	break;
case 1100:
	sun.transform.Rotate (1,0,0);
	break;
case 1200:
	lightRotation = Vector3 (90, 0, 0);
	sun.transform.Rotate (lightRotation);
	timeofday++;
	break;
case 1300:
	sun.transform.Rotate (1,0,0);
	break;
case 1400:
	sun.transform.Rotate (1,0,0);
	break;
case 1500:
	sun.transform.Rotate (1,0,0);
	break;
case 1600:
	sun.transform.Rotate (1,0,0);
	break;
case 1700:
	sun.transform.Rotate (1,0,0);
	break;
case 1800:
	sun.transform.Rotate (1,0,0);
	break;
case 1900:
	sun.transform.Rotate (1,0,0);
	break;
case 2000:
	sun.transform.Rotate (1,0,0);
	break;
case 2100:
	sun.transform.Rotate (1,0,0);
	break;
case 2200:
	sun.transform.Rotate (1,0,0);
	break;
case 2300:
	sun.transform.Rotate (1,0,0);
	break;
}
}

now I don’t mind having to hard code all 24 hours of the day, as opposed to having an automatically scripted rotation, cuz time will pass by the hour in the game as you complete activities. My problem here is rotating directional lights. You can see in case 1200 i have the light rotating 90 degrees on it’s X axis, when in fact i need to set it’s rotation to 90. How can i do this, i tried all the methods in the script reference like eulerAngles, and localEulerAngles, but i still can’t get it. any suggestions?

Found my beef, setting rotations has to be done using eulerAngles, like so:

	lightRotation = Vector3 (90, 0, 0);
	sun.transform.eulerAngles = lightRotation;

which will actually SET the rotation on the X axis to 90, as opposed to

	lightRotation = Vector3 (90, 0, 0);
	sun.transform.Rotate = lightRotation;

which will actually ROTATE the transform relative to it’s current position, correct? Like I said i’m pretty new so any help is much appreciated…

I believe you are correct.

If you want to set the rotation, you need to do so using the properties, like transform.rotation (a quaternion) or transform.eulerAngles.

If you use the Rotate() function, then you will be rotating relative to the previous rotation stored on the object.

This is similar to using the transform.Translate() function on an object as compared to the transform.position property. transform.Translate() on an object moves it relative to its last position, where transform.position will just set the position, no matter what its original position is.

Now what does a Quaternion look like, and how do i use that to set a rotation should i ever have the need? BTW heres my final code minus some color tweaks:

//======================================//
//
//			  Daylight System
//			(c)2010 Bill Hayes
//
//======================================//

// Variable Declarations ================//

var sun : Light;
var timeofday : float;
var azimuth : int;
var elevation : int;
var brightscale : float;
var lightRotation : Vector3;
private var lightColor : Vector4;


//Function Declarations =================//

function IncrementTime()
{
if (timeofday < 2300){
	timeofday+=100;
	}
	else{
	timeofday = 0;
	}
}


function Update ()
{

//== Increment Time (DEBUG) ==========//
if (Input.GetKeyUp("e"))
{
IncrementTime();
}


switch(timeofday)
{
// == Midnight ==
case 0:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0;
	break;
case 100:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0;
	break;
case 200:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0;
	break;
case 300:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0;
	break;
case 400:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0;
	break;
case 500:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0;
	break;
	
// == Dawn ==
case 600: 
	lightRotation = Vector3 (30, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.002;
	lightColor = Vector4 (100, 100, 100, 0);
	sun.color=lightColor;
	break;
case 700:
	lightRotation = Vector3 (40, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	lightColor = Vector4 (100, 100, 100, 0);
	sun.color=lightColor;
	break;
case 800:
	lightRotation = Vector3 (50, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	lightColor = Vector4 (100, 100, 100, 0);
	sun.color=lightColor;
	sun.intensity=0.003;
	break;
case 900:
	lightRotation = Vector3 (60, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	lightColor = Vector4 (100, 100, 100, 0);
	sun.color=lightColor;
	sun.intensity=0.005;
	break;
case 1000:
	lightRotation = Vector3 (70, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	lightColor = Vector4 (100, 100, 100, 0);
	sun.color=lightColor;
	sun.intensity=0.007;
	break;
case 1100:
	lightRotation = Vector3 (80, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	lightColor = Vector4 (100, 100, 100, 0);
	sun.color=lightColor;
	sun.intensity=0.009;
	break;
	
// == High Noon ==
case 1200: 
	lightRotation = Vector3 (95, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.009;
	lightColor = Vector4 (100, 100, 100, 0);
	sun.color=lightColor;
	break;
case 1300:
	lightRotation = Vector3 (100, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	break;
case 1400:
	lightRotation = Vector3 (110, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	break;
case 1500:
	lightRotation = Vector3 (120, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	break;
case 1600:
	lightRotation = Vector3 (130, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.007;
	break;
case 1700:
	lightRotation = Vector3 (140, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.006;
	break;
case 1800:
	lightRotation = Vector3 (150, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.005;
	break;
case 1900:
	lightRotation = Vector3 (160, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.003;
	break;
case 2000:
	lightRotation = Vector3 (170, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.000;
	break;
case 2100:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.000;
	break;
case 2200:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0.000;
	break;
case 2300:
	lightRotation = Vector3 (-90, 0, 0);
	sun.transform.eulerAngles = lightRotation;
	sun.intensity=0;
	break;
}
}

Now i just need to attach it to my clockGUI (need to figure out some more GUI scripting first), and I was also wondering if i could set different lights to a common tag or layer named “NightLights” or something and adjust them all at once? That would be super dope…

If i were you, maybe i will try to make a daylight system involving trigonometry.
Why don’t you emulate the rotation of earth, moving the sun around the scene?.. 15 degrees = 1 hs :smile:

You could change the tint at certain dregrees to get the afternoon look and feel (and of course, you would do a good use of dynamic shadows). Think about it.

Scripting Guide
More info

Yes, you can tag them then retrieve an array of them with GameObject.FindGameObjectsWithTag(). Then just iterate through the array to update them all.

@kokumo - I will definitely look into it, thanks for the equation to i was curious how far the sun travels in an hour, maybe once im a bit better, this will suffice for now.

@laurie - Lol i looked at both those pages while i was studying this, i’m still not sure how it differs from Euler, i suppose i’d have to see them compared in code. As for the nightlights suggestion, I appreciate it. I had a sneaking suspicion I would use tags to define them, i was just unsure as to the syntax.

Thanks a ton guys for all helpful advice and swift responses!!!