- Home /
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.
Answer by smirlianos · Jun 20, 2013 at 11:01 AM
var sun : GameObject;
function OnTriggerEnter() {
sun.light.intensity = 0.1;
}
function OnTriggerExit() {
sun.light.intensity = 1;
}
There are so many same questions out there...
i am wondering how to make the effect slowly!! like ease in and ease out effect... i have searched but cant find the proper solution...
Answer by AlucardJay · Jun 25, 2013 at 11:39 AM
For future readers : you can get tricky with InvokeRepeating and CancelInvoke !
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.CancelInvoke.html
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" );
}
}
Your answer
Follow this Question
Related Questions
how to collide while having a trigger? 2 Answers
Objects Touching? 1 Answer
Move player after colliding with trigger? 1 Answer
Trigger problems... 1 Answer