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.

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:

  1. Create a new Scene and delete the default "Main Camera".
  2. Create a new Cube and call it "Floor". Scale it so it becomes your floor.
  3. 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".
  4. Create a new Light and call it "Light". Place it somewhere you can see its light.
  5. 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".
  6. 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.

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