- Home /
how to turn a lamp on with a switch?
Hi guys,
how can I make a lamp in the ceiling turn on when I press E on a switch in a wall?
Thanks
"Press E on a switch in a wall"? What does it means? 1- Press the "E" key, 2- click some switch object or 3- collide with the switch object 4- none of the options?
I mean that I have a switch on a wall, and a lamp in ceiling. And I want the lamp to turn on when I approach the switch and press "E" on the it.
Answer by Meltdown · Jun 21, 2011 at 11:27 AM
Take a look GetKeyDown to determine if 'E' was pressed.
With regard to turning the light on or off, you can find the light in your scene using GameObject.Find, and setting its renderer component to enabled or disabled.
Edit : To determine if the player is within range of the switch, you can add a trigger to the area in front of the switch, and if the player is in that area, you know which switch/light to turn on/off.
Alternatively you can do a raycast from your players forward direction to determine if they are facing the switch, and check that in your script.
Your answer
Follow this Question
Related Questions
Raycast switching object's color on and off. 1 Answer
Toggle GUI button 1 Answer
Multiple Camera.main.ScreenPointToRay 1 Answer
Switching cameras? 1 Answer
How to turn on a light, then off with the same button. 1 Answer