- Home /
Spotlight switch script?
Hello fellow game devs! :)
i'm creating a showroom and i have made 4 spotlights tilted in the rifght angle for illuminating the presented object.
I need to write a script where the user is able to switch each of the 4 spots on and off.
i am relatively newb if it comes to manipulating game objects...do you guys have any advice?
thanks in advance
h4wkeye
EDIT: The User is walking through the showroom using a standard FPS prefab.
Answer by Statement · Mar 29, 2011 at 12:29 PM
You could make an Interacter script that you add to your camera that send OnInteract on any ray hit. Then you can easily make a switch for your light(s). Just add a collider to the switch and set up target, range, proper layer masks and you're ready to go. This can be used to do all sorts of interactions.
Interacter.js (Place on Camera)
var interactLayers : LayerMask = -1; var interactRange : float = 2.0f;
function Update() { if (Input.GetKeyDown(KeyCode.E)) { var hit : RaycastHit; if (Physics.Raycast (transform.position, transform.forward, hit, interactRange, interactLayers)) hit.collider.SendMessage("OnInteract", gameObject, SendMessageOptions.DontRequireReceiver); } }
LightSwitch.js (Place on button that has collider)
var target : Light;
function OnInteract() { target.enabled = !target.enabled; }
Step by step tutorial:
- Create a new Scene and delete the default "Main Camera".
- Create a new Cube and call it "Floor". Scale it so it becomes your floor.
- Drag in "First Person Controller" prefab into your scene and place it on your floor.
-
- Set layer to "Ignore Raycast" on your "First Person Controller" object (And all children).
-
- Add Interacter script to "First Person Controller/Main Camera". Click Continue.
-
- Set Interacter.interactLayers to every layer except "Ignore Raycast".
- Create a new Light and call it "Light". Place it somewhere you can see its light.
- Create a new Cube and call it "Button". Place it somewhere you can reach it.
-
- Add LightSwitch to "Button" and set its target to "Light".
- Play the game. Walk up to the button and press E. It should now toggle off and on.
I did just this and it works fine here.
If you want to know the gameObject which called OnInteract, you can just change the signature of the function to function OnInteract(interacter : GameObject) { }. Note that this would return the gameObject of the camera in your case.
I cannot get it to work i gave the cube a raycast collider and checked "is trigger" then i attached the script to the cube it wont work D:
It's probably because the camera hit the character capsule. I had this problem too. You need to adjust the layer mask so it doesn't include the character. You could make this easy by creating a new layer mask for your character and have the interacter ignore it. That's the primary reason I added the variable interactLayers.
Answer by Uzquiano · Mar 29, 2011 at 12:05 PM
Hi,
Have you take a look to the documentation?
Just put the intensity to 0 in the update()
light.intensity = amplitude;
Do you want to activate and desactivate the light when the player is around? Just add a trigger event with a collider, there are many examples in here if you do a search for colliders. Or in the documentation, for examlple here.
Hope this help you
i've found a better solution for my problem. thank you anyway man :)
It would be helpful if you told us about your solution. This is a collaborative community :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613115505im_/https://answers.unity.com/themes/thub/images/avi.jpg)