How do you make Post Processing Stack Depth of Field auto-focus on an object?

How do I make it focus on whatever is the nearest object to the camera.

1 Like

Check out this repository: GitHub - keijiro/PostProcessingUtilities: Utility scripts for the post processing stack

Then you need to modify the script to shot a ray from the camera to see what the object in front of the camera is and what’s its depth is.
I recommend using Physics.RaycastNonAlloc in order to avoid garbage.
Here is a part of my script that should point you in the right direction:

private void CalculateDistanceToVisibleObject()
{
    ray.origin = transform.position;
    ray.direction = transform.forward;
    hits = Physics.RaycastNonAlloc(ray, results, maxDistance);
    var distance = (hits > 0) ? results[0].distance : maxDistance;
    targetPosition = transform.position + transform.forward * distance;
}
3 Likes

I still remember the days when we could JUST drag/assign a target object in inspector or via script…

2 Likes

You can still use the older effects. The DoF effects work fine in 2018.1 with the newer stack, plus, you get sun shafts.

2 Likes

good to know it works!. The thing is that using non stac FX does not allow theese to run in the correct order, hope using DOF befor or after the stack does not break anything. Sunshafts is also something I miss but I found a dev implemented theese as stack extension. (available on the store).

Curious, which store asset are you using? Thanks!

I’ve not tested theese, seem more up to date
https://assetstore.unity.com/search?q=legacy&k=legacy

Let me try this one, having the same issue though. Will visit the site. Having my own script to resolve the problem doesn’t help at all. Thanks to this. Go2top.

Best would be to be able to just drag and drop or select something from a list. OR in a smarter tool where you could provide a list of objet that would always become the focus when you are in proximity. That could be part of the Cinemachine if it isn’t already.

I confirm the old script still work son 2018.2 and on Xbox One. Unity is doing crazy things overall, now they’ve *ucked up the whole prefab system.