- Home /
How to change Focus Distance at run time? Using PostProcessing Depth of Field.
EDIT: Good news everyone! I figured out what I was doing wrong. Simply... I just was referencing the wrong PostProcessingVolume. I was referencing the one on my camera, and there already was a GameObject in the scene with a PostProcessingVolume on it. Referencing that one with my same code works like a charm. I feel like an idiot. Haha! Happy coding all. :) -Gaff
Hi I'm using Unity 2019.1.1f1 and the LWRP.
I want to change the Focus Distance of the Depth of Field Effect using Post Processing.
I've read all of the numerous posts on here and still don't have an answer.
With the following code and a properly setup Profile on a PostProcessingVolume on my camera I can get the Focus Distance to update and change in the inspector properly during run time, but the actual depth of field does not change from it's original setting when the game began?
[SerializeField]
private PostProcessVolume postProcessVolume;
[SerializeField]
private DepthOfField DOF;
void Awake ()
{
postProcessVolume = camera.gameObject.GetComponent<PostProcessVolume>();
postProcessVolume.profile.TryGetSettings(out DOF);
}
void Update ()
{
RaycastHit hitInfo;
Ray rayDOF = new Ray(camera.transform.position, camera.transform.forward);
if (Physics.Raycast(rayDOF, out hitInfo))
{
FloatParameter newFocusDistance = new FloatParameter { value = Vector3.Distance(camera.transform.position, hitInfo.point) };
DOF.enabled.value = true;
DOF.focusDistance.value = newFocusDistance;
}
else
{
DOF.enabled.value = true;
DOF.focusDistance.value = 200f;
}
}
Do I have to apply this effect after making changes? Is there some method to call?
What am I missing here?
I've searched everywhere and can't find the answer. Thanks all! :)
Gaff
Answer by Old-Meiser-Gaff · Aug 03, 2019 at 03:12 AM
EDIT: Good news everyone! I figured out what I was doing wrong. Simply... I just was referencing the wrong PostProcessingVolume. I was referencing the one on my camera, and there already was a GameObject in the scene with a PostProcessingVolume on it. Referencing that one with my same code works like a charm. I feel like an idiot. Haha! Happy coding all. :) -Gaff
Your answer
Follow this Question
Related Questions
make your camera see through an Image!! 2 Answers
Get Camera Current Post Profile Name 0 Answers
How to change the color of a vignette? 0 Answers
Modify values on VolumeProfile URP. 1 Answer
Capture Screen with post effect 0 Answers