[CLOSED] Light switches and Trigger distance.

Basically i have searched over the mighty realm of google and i still have not found an answer to this.

my question involves a light switch and a box collider marks as trigger.

the script works but, i would like to know how i could check for a certain distance BEFORE turning on the light.

at the moment, i can turn lights on and off from say… “2Miles” away… which i do not want.

this is a ‘FPS’ - First Person Shooter type of game.

then here is the script i use for my light switches.

var linkedLight : Light;
 
function OnMouseDown()
 {
        linkedLight.enabled = !linkedLight.enabled;
   }
 
function LightOff (){
 linkedLight.enabled = false;
} 

if anyone has an idea how i could achieve this, i would greatly appreciate it.

thank you

  • Ownerfate

Use a trigger box around your light. Add the script below:

function OnTriggerEnter(col:Collider){
   if(col.gameObject.tag == "Player") // light on
}

i figured it out, it was the function.

instead of " OnTriggerEnter() "

it was " OnTriggerStay() "

CODE:

var linkedLight : Light;
 
function OnTriggerStay()
 {
        if(Input.GetKeyDown("e"))
        {
			linkedLight.enabled = !linkedLight.enabled;
   }
}
 
function LightOff (){
 linkedLight.enabled = false;
}