In this lesson, you will learn how to manipulate the Depth of Field volume override to guide player attention by dynamically blurring objects based on their proximity to the camera.
Understanding Depth of Field
Depth of Field (DoF) simulates the lens characteristics of a real camera, where only objects within a specific range of distances remain in sharp focus. By blurring the foreground and background, you create a visual hierarchy that forces the player to focus on the subject, such as a quest-critical item or a narrative-heavy character.
Configuring the Volume Profile
To implement this, ensure your scene has a Volume component. Add the Depth of Field override. You will primarily interact with three parameters:
- Mode: Use
Gaussianfor a performance-friendly blur orBokehfor a high-fidelity, physically-accurate lens effect. - Focus Distance: This determines exactly how far from the camera (in units) the point of perfect focus resides.
- Aperture/Blur Size: Controls the intensity of the blur; a smaller aperture (higher f-stop) keeps more of the scene in focus, while a larger aperture (lower f-stop) creates a shallower, more dramatic depth of field.
Making Focus Dynamic
Static focus is rarely enough for dynamic gameplay. To shift focus during an event—like a cutscene or interaction—you can manipulate the DepthOfField component via script.
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class FocusPuller : MonoBehaviour
{
public Volume globalVolume;
private DepthOfField dof;
void Start() {
globalVolume.profile.TryGet(out dof);
}
public void UpdateFocus(float distance) {
// Adjust the focus distance to follow an object
dof.focusDistance.value = distance;
}
}
By linking the focusDistance.value to the distance between your Camera.main.transform and a target object (often found using Vector3.Distance), you can create a "focus pull" effect. This ensures that as your target moves or as the camera pans, the background remains softly obscured, keeping the composition intentional and professional.
Key Takeaway: Use the Depth of Field volume override to define a narrow focus plane, and programmatically update the focus distance to keep the player’s eye locked onto your scene’s focal point.