- Home /
Changing Image Effect 'Depth Of Field' in run time
I'm having trouble with accessing the 'Depth of Field' image effect that's placed on the main camera in my scene.
I'm trying to change two things the "Focus On Target" and also the effect amount so I can fade it completely away. (I'm aware that there isn't a current Effect Amount variable so please could someone explain how I could go about adding this to the depth of field effect)
Any ideas? Cheers - C
does anybody know how to access the rest of the dof values? i want to change "backgroundblur and foreground spread" via script?
Answer by DaveA · Apr 18, 2011 at 08:59 PM
I think you would just assign 'ObjectFocus' to whatever object you want to focus on, something like
Camera.main.GetComponent(DepthOfField).objectFocus = myCoolObject; // or myCoolObject.transform?
and the amount of effect seems to be 'focal size' (except at zero it's more intense) so maybe:
if (amount > 0)
{
Camera.main.GetComponent(DepthOfField).enabled = true;
Camera.main.GetComponent(DepthOfField).focalSize = amount;
}
else
Camera.main.GetComponent(DepthOfField).enabled = false;
Thanks great work on the Effect Amount makes a nice quick fix, one problem Object Focus is not found :/
Really? I just coded something almost exactly like this yesterday, I thought it was objectFocus...
$$anonymous$$aybe i'm using some outdated Unity assets, it's focusOnThis or objectFocus depending on your unity assest version for anyone who is trying this.
Answer by MaxvH · Dec 08, 2012 at 04:08 AM
I don't know what Unity version you guys are using, but there is no "Depth Of Field" script in Unity 3.5 or Unity 4. There is a "Depth Of Field (34)", a "Tilt Shift", and a "Depth Of Field (Scatter)". Is there something I'm missing, like a built in script or something? Or did you just write "DepthOfField" because different people use differnet scripts? I'm mentioning this not because I'm that jerk who corrects people or anything but because the script is not working for me and I am wondering if there is an obvious reason why. The "amount" in my script is the value of a knob from the iGUI user interface plugin. Here is the script I have:
public void knob1_ValueChange(iGUIKnob caller){
if (knob1.value > 0)
{
Camera.main.GetComponent(DepthOfField34).objectFocus = gameObject.transform;
Camera.main.GetComponent(DepthOfField34).enabled = true;
Camera.main.GetComponent(DepthOfField34).focalZDistance = knob1.value;
}
else
Camera.main.GetComponent(DepthOfField34).enabled = false;
}
So what I'm wondering is, is there something obviously wrong with my script, or is this just because my version of unity is too new? And yes I did add the component to the main camera and I did use the right variables (I think).
Thanks/hope my comment was somewhat helpful! -Max
Your answer
