How to code lights to turn on when its dark?
I want to make some street lamps turn on when its dark but its not working.
Are we talking street lights? car lights? robot lights? First you need some sort of condition you can check, like if it is a car moving through a tunnel then you could use a trigger zone or if it is street lights you could use a game clock etc.
Am I doing anything wrong? Here are my two scripts used;
1)Supposed to rotate the sun (script is attached to it) on its x-axis by .1;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimePass : $$anonymous$$onoBehaviour {
private Transform SunPos;
// Use this for initialization
void Start () {
//Have the var SunPos be the attached object's transform
SunPos = this.gameObject.transform;
}
// Update is called once per frame
void Update () {
SunPos.Rotate(.1f,0,0);
//if it's day again (meaning the sun has done a complete rotation (meaning it has rotated 360 degrees)); reset it's rotation on the x-axis
if(SunPos.rotation.x == 360){
SunPos.rotation.x.Equals (0);
}
//Have this here because once it reached 180 it would go negative
if (SunPos.rotation.x < 0f) {
SunPos.rotation.x.Equals(SunPos.rotation.x * -1);
}
}
}
2) Code for the lights (attached) to turn on when the sun's rotation on the x-axis is greater than 184
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OnlyTurnOnAtNight : $$anonymous$$onoBehaviour {
public Transform Sun;
private Light light;
// Use this for initialization
void Start () {
//Store the attatched game object's light component into the variable; Light
light = this.gameObject.GetComponent<Light>();
}
// Update is called once per frame
void Update () {
//Print to the console if the sun's rotation is greater than 184
print (Sun.rotation.x > 184f);
if (Sun.rotation.x > 184f) {
light.enabled = true;
}
if(!(Sun.rotation.x > 184f)){
light.enabled = false;
}
}
}
In your script you look to be using rotation.x, rotation itself is a quaternion and not euler angles, so I wouldnt expect a 0-360 value from that. try checking sun.eulerAngles.x ins$$anonymous$$d.
Your answer
Follow this Question
Related Questions
Is 1 unit of light.intensity equal to 1 lux in real life? (NOT using HDRP!) 0 Answers
Spotlight becomes square. -1 Answers
unable to make a black scene 0 Answers
Everything is too bright 1 Answer