Hello all,
I’ve setup a directional light to revolve around the X axis to make a day/night cycle and I want to control some street lights to come on when the “Sun” is down and turn off when the Sun comes up.
So I wrote two scripts to do this BUT obviously it doesn’t work. in the inspector when I play the directional rotation script the X axis increases until it reaches ~180 degrees then converts to ~179 degrees. In the inspector the sun rises a 0 on the X axis and sets at the ~180/-179 position
I figured I could just do something like if > 0 turn light off and if < 0 turn light on. this kinda worked. It would turn on for one complete rotation, then turn off for one complete rotation. I changed the code now it doesn’tt seem to work at all.
Could someone tell me what i am doing wrong please.
here is my code
sun rotation script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spin : MonoBehaviour {
public float rate = 10.0f;
private float x = -15.0f;
void Update ()
{
x += Time.deltaTime * rate;
transform.rotation = Quaternion.Euler(x, 0, 0);
}
}
Light switch script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class switchOn : MonoBehaviour {
public GameObject sun;
private Light streetLight;
void Start ()
{
streetLight = GetComponent<Light>();
streetLight.enabled = false;
}
void Update ()
{
if(streetLight.enabled == true)// turn off
{
if (sun.transform.rotation.x > 0)
{
streetLight.enabled = false;
}
}
if (streetLight.enabled == false)//turn on
{
if (sun.transform.rotation.x < 0)
{
streetLight.enabled = true;
}
}
}
}