- Home /
Day-night changing via rotating Directional Light
Hello, I am trying to make rotating Directional Light for changing 2 skyboxes depending on transform.rotation.x value, but it doesn't work. It is ignoring IF/ELSE statement. Anyone know how could I make it? Here is the script for better understanding:
#pragma strict
var daySpeed : float;
var DaySkybox : Material;
var NightSkybox : Material;
RenderSettings.skybox = NightSkybox;
function Start() {
daySpeed = 0.1;
}
function Update() {
transform.Rotate(daySpeed, 0, 0);
if(transform.rotation.x > 0 && transform.rotation.x < 270) {
RenderSettings.skybox = DaySkybox;
}
else{
RenderSettings.skybox = NightSkybox;
}
}
forgot mentioned, that it should rotate until 360 degrees, then set value to 0 and doing this process again...
Answer by Hyperion · Aug 04, 2013 at 06:49 PM
Might I suggest not using a rotation? That will cause trees to stay 'alight'. Here's code for intensity switching.
//Duration of day
var duration : float= 500.0;//day is going to change ULTRA slowly
function Update() {
// argument for cosine
var phi : float = Time.time / duration * 2 * Mathf.PI;
// get cosine and transform from -1..1 to 0..1 range
var amplitude : float = Mathf.Cos( phi ) * 0.5 + 0.5;
// set light color
light.intensity = amplitude;
}
Then you could change your sky box based on intensity.
@DaveA Thank you, and @$$anonymous$$agnomous if my answer mostly solved your problem, please accept it. If you have any questions, please let me know.
Thank you for help, but I wonder how to make day not so long. I tried editing duration variable, but doesn't matter if it is 0.00001 or 5000, day is always too long.
Hmm, that's really queer. I've managed to make lights flash using this script...... Just wondering, when you put 5000 in, did you type it as a float? (5000.0) But I don't think putting a big number in would help..... And I'm guessing you tried 1.0... OOOOHHH! Ok, basically the duration originally should be 1.0 all the way to 8.0. Try every single number between and including those, and tell me the results.
It works, now I can mark your answer as correct, thank you, man! :-)
Your answer
Follow this Question
Related Questions
Help with GuiText flickering code. 1 Answer
Day Night Cycle Script Not Working? 1 Answer
How do I loop my light animation is javascript? 1 Answer
Changing scene messes up lights 6 Answers
Light problems 1 Answer