- Home /
Press key to toggle pointlight on/off
Hey guys. I tried to make an area were the player can toggle a pointlight on/off. I made a box collider and i enabled the trigger. If you're inside the box collider. There will come a text with. Press "L" to toggle light. And if the pointlight is on. And you leave the box collider. The pointlight will turn off.
Someone understand this and can help me?
Thanks in advance..
Answer by aldonaletto · Dec 23, 2011 at 12:46 AM
You should set a boolean flag in OnTriggerEnter, and clear it in OnTriggerExit. This boolean flag could be used in OnGUI to enable a GUI.Label with the text, and in Update to enable the light and the Input.GetKeyDown to toggle the light on/off - something like this (trigger script):
var pLight: Transform; // drag the point light here private var enableLight: boolean = false;
function OnTriggerEnter(col: Collider){ if (col.tag == "Player") enableLight = true; }
function OnTriggerExit(col: Collider){ if (col.tag == "Player") enableLight = false; }
function OnGUI(){ if (enableLight){ GUI.Label(Rect(10,Screen.height-50, 300, 40), "Press L to toggle light"); } }
function Update(){ if (enableLight){ // if enableLight is true... if (Input.GetKeyDown("l")){ // and L pressed... pLight.light.enabled = !pLight.light.enabled; // toggle light } } else { // turn light off if enableLight is false pLight.light.enabled = false; } }
Your answer
Follow this Question
Related Questions
Audio Source will not "unmute" after toggling? 2 Answers
Check GUI toggle button with the M keyboar Key 2 Answers
Toggle bool with keypress -2 Answers
Toggling with a key not working 2 Answers
Toggling through music 1 Answer