- Home /
Delta component
I have a part of a script that I found on another question.
function OnTriggerEnter(other:Collider) { if(other.name=="Player") //Player entered the trigger Camera.main.GetComponent(MotionBlur).enabled=true; }
Im using it to trigger a component (In my case an image effect) to enable itself and disable when I touch an object. So my question now is, How do I go about setting up the script so that, when you touch an object the Component slowly fades in, stay on for a minute, then slowly fade out again.
Any one have any ideas of how i would go about this. My guess is it will deal with some kinda delta? Please please help somebody, I really have no idea how to script on my own.
Answer by hijinxbassist · Jun 19, 2012 at 11:13 PM
You will use Lerp to ease the values of the blur in. A good way to do this is to set all the values like normal in the inspector. In the script make a var for each float value of the image effect. Then in start set those values to 0.
private var fading:boolean;
private var canFadeIn:boolean=true;
private var blurSpread:float;
private var orgBlueSpread:float;
private var blur:Blur;
function Start()
{
blur=Camera.main.transform.GetComponent(Blur);
orgBlurSpread=blur.blurSpread;
blur.blurSpread=0;
}
function OnTriggerEnter(other:Collider)
{
if(!fading)
{
if(canFadeIn)Fade(false);
else Fade(true);
}
}
function Fade(out:boolean)
{
fading=true;
var t:float=0;
var lerpTime:float=1.5;
var start:float;
var target:float;
if(out)
{
start=orgBlurSpread;
target=0;
canFadeIn=true
}
else
{
start=0;
target=orgBlurSpread;
canFadeIn=false
}
while(t<1)
{
blur.blurSpread=Mathf.Lerp(start,target,t);
t+=Time.deltaTime/lerpTime;
yield;
}
fading=false;
}
on line 23 it says, Unexpected token: in also on line 23 it says, Expecting EOF, found boolean
"in" is propably a bad variable name, try something else ;)
in is actually a keyword which is used in a for-each loop
hahahahaH! wow, totally missed that one, dumb mistake Thanks
Amazing it works! There are no errors! But it works in a strange way haha Right now, when you touch the cube, the effect fades in as expected But it doesnt fade itself out, you have to touch the cube again to make it fade out. When I read your script, it appears as if it says. the effect will fade in, stay on for awhile, then slowly fade back out. (which would function much nicer if possible).
fading=true;
var t:float=0;
var lerpTime:float=1.5;
var start:float;
var target:float;
for(var i:int=0;i<2;i++)
{
if(i==0)
{
start=0;
target=orgBlurSpread;
canFadeIn=false
}
else
{
start=orgBlurSpread;
target=0;
canFadeIn=true
}
while(t<1)
{
blur.blurSpread=$$anonymous$$athf.Lerp(start,target,t);
t+=Time.deltaTime/lerpTime;
yield;
}
//This is a delay for the Fade Out portion 'if "i" equals zero'
//means we just faded the effect in, so we need to delay
//the next run of the previous code(for i<2, "(i=0,i=1) ==2 loops")
//next time (when i==1) this delay wont happen bcz theres no reason
if(i==0)
{
var nt:float=0;
while(nt<1)
{ //fadeOutDelay is how long until fade out starts
nt+=Time.deltaTime/fadeOutDelay;
yield;
}
}
}
fading=false;
Your answer
Follow this Question
Related Questions
Need some help an object collider that stops a timer then displays it on another scene 1 Answer
Character controller not fitting through door 0 Answers
Need help with game points and trigger 2 Answers
Fading Object in and out with timer 1 Answer
Start/Stop Timer when player enters/exits a specific space? 1 Answer