- Home /
access DOF values via script
does anybody know how to access the Depth Of Field image effect values? I want to change backgroundblur and foreground.spread" in runtime via script? and tried allot like
GameObject.Find("camera").GetComponent(DepthOfField).backgroundBlurSpread = 1;
but i can't figure out the right way...
Answer by save · Jun 22, 2011 at 11:33 PM
Try doing this
private var dof : DepthOfField;
function Start () {
dof = Camera.main.GetComponent(DepthOfField);
}
Then access foreground- and background blur via:
dof.foregroundBlurSpread = value;
dof.blurSpread = value;
Doing this will go easier on the CPU.
If it's a focal length based on screen center you want to achieve, here's an example:
//Create an empty GameObject and assign it to focusPoint in the Inspector
//then assign it to the Object Focus on your Depth of Field script.
var focusPoint : Transform;
function Update () {
//Focal distance
var focalHit : RaycastHit;
var focalRay : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2,0));
if(Physics.Raycast (focalRay, focalHit, 100)) {
focusPoint.position = Vector3.Lerp(focusPoint.position, focalHit.point, Time.deltaTime*10);
} else {
focusPoint.position = Vector3.Lerp(focusPoint.position, transform.position+Vector3(0,0,100), Time.deltaTime*10);
}
}
If you're uncertain of variable names you can always have a look in the script file (DepthOfField.js).
You really should type your variable if you talk about making the code faster ;)
private var dof : DepthOfField;
Your answer
![](https://koobas.hobune.stream/wayback/20220612114905im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Excluding objects from depth of field. 2 Answers
Unity 5 Standard Shader and DoF Effect 0 Answers
Water Blur??Doesnt work 0 Answers
How to add motion blur effect to your camera 1 Answer
Enemy attacks when looked at 2 Answers