- Home /
Automatically disabling, enabling and looping Depth of field
Hi,
i want to automatically disable and then enable Depth of field with looping forever, the script is attached to my main camera. I want the transfer from enabled to disabled position to be smooth and than loops forever.
Is there some preset function in unity, or do i have to somehow implement it in my script manually?
thanks.
ok, i have managed to work it on prressing spacebar, but how do i make it loop?
#pragma strict
private var myLight : DepthOfFieldScatter;
function Start ()
{
myLight = GetComponent(DepthOfFieldScatter);
}
function Update ()
{
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))
{
myLight.enabled = !myLight.enabled;
}
}
Answer by robertbu · Sep 29, 2014 at 04:57 PM
If you just want to turn it on an off at some interval, see InvokeRepeating().
#pragma strict
private var myLight : DepthOfFieldScatter;
function Start ()
{
myLight = GetComponent(DepthOfFieldScatter);
InvokeRepeating("FlipLight", 2.0, 2.0);
}
function FlipLight ()
{
myLight.enabled = !myLight.enabled;
}
Thanks, that works.
Is there any way to make the change smooth? I think i need to somehow implement some variables and make them slowly count to target value and than back to make the change smooth.
It depends on what you want. Based on the reference (and untested), something like this might work:
#pragma strict
public var speed = 1.0;
public var $$anonymous$$ = 2.0;
public var max = 100.0;
private var delta : float;
private var myLight : DepthOfFieldScatter;
function Start ()
{
myLight = GetComponent(DepthOfFieldScatter);
delta = max - $$anonymous$$;
}
function Update ()
{
myLight.focalSize = $$anonymous$$ + ($$anonymous$$athf.Sin(Time.time * speed) + 1.0) / 2.0 * delta;
}
This cycles the focal size. I'm not sure what values are right for speed, $$anonymous$$ and max since I cannot test it right now. Note you must change the values in the Inspector, not in code.
Thanks, that works perfectly. Just values are diffrent, i set speed to 0.05, $$anonymous$$ to 1 and max to 1.2
thanks a lot
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can't access Depth of Field? 2 Answers
Play sound while moving. 2 Answers
How can i auto Position my Game Object? 0 Answers