decrease light intensity on collide trigger!

hi i am having problem with controlling a directional light intensity with script.

i have a directional light named sun and a trigger. when the player is inside the trigger the intensity of the sun should decrease, and when get out from trigger the intensity should reset to it’s previous value… please guys help! need this immediately.

thnx.

var sun : GameObject;

function OnTriggerEnter() {
    sun.light.intensity = 0.1;
}

function OnTriggerExit() {
    sun.light.intensity = 1;
}

There are so many same questions out there…

try googling first :slight_smile:

For future readers : you can get tricky with InvokeRepeating and CancelInvoke !

for example :

#pragma strict

var sun : GameObject;

var fadeRate : float = 1.0; // time in seconds

function OnTriggerEnter( other : Collider ) 
{
	if ( other.gameObject.name == "Player" )
	{
		CancelInvoke( "IncreaseIntensity" ); // make sure the other function stops being called
		
		InvokeRepeating( "DecreaseIntensity", 0.02, 0.02 ); // sun goes down
	}
}

function OnTriggerExit( other : Collider )  
{
	if ( other.gameObject.name == "Player" )
	{
		CancelInvoke( "DecreaseIntensity" ); // make sure the other function stops being called
		
		InvokeRepeating( "IncreaseIntensity", 0.02, 0.02 ); // sun comes up
	}
}

function DecreaseIntensity() // sun goes down
{
	sun.light.intensity -= 0.02 / fadeRate;
	
	if ( sun.light.intensity <= 0 )
	{
		sun.light.intensity = 0;
		
		CancelInvoke( "DecreaseIntensity" );
	}
}

function IncreaseIntensity() // sun comes up =]
{
	sun.light.intensity += 0.02 / fadeRate;
	
	if ( sun.light.intensity >= 1.0 )
	{
		sun.light.intensity = 1.0;
		
		CancelInvoke( "IncreaseIntensity" );
	}
}