Skip to content

A URP Unity script that dynamically adjusts camera Depth of Field in real time to keep the focal point perfectly in focus.

License

Notifications You must be signed in to change notification settings

hsuehyt/AutoFocus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

AutoFocus for Unity URP

A URP Unity script that dynamically adjusts camera Depth of Field in real time to keep the focal point perfectly in focus. Designed to work seamlessly with the companion script CameraController.


๐ŸŽฏ Overview

AutoFocus.cs reads the focal point assigned in CameraController.cs and dynamically adjusts the Depth of Field (DoF) parameter in a URP Global Volume. This keeps the selected object or scene point perfectly in focus โ€” ideal for cinematic orbit cameras, XR/VR rigs, and animated camera moves.


โš™๏ธ Features

  • Works with URP Volume Depth of Field component
  • Automatically detects and syncs with your CameraController
  • Adjusts focus distance every frame
  • Zero dependencies โ€” plug and play
  • Compatible with Unity 2022+ URP

๐Ÿงฉ Requirements

  • Unity URP project
  • A Global Volume with a Depth of Field override enabled
  • CameraController.cs (download from here)

๐Ÿง  How It Works

  1. The script accesses the DepthOfField component from your Volume Profile.
  2. It gets the current focalPoint from the attached CameraController.
  3. Calculates the distance between the camera and that focal point.
  4. Updates dof.focusDistance in real-time.
float distance = Vector3.Distance(transform.position, focalPoint.transform.position);
dof.focusDistance.value = distance;

๐Ÿš€ Setup Guide

  1. Assign URP Asset

    • In Project Settings โ†’ Graphics, assign your URP pipeline asset.
    • Enable Post Processing and Depth Texture.
  2. Add a Volume

    • Create a Global Volume โ†’ Add Override โ†’ Post-processing โ†’ Depth of Field.
    • Set Mode to Bokeh and tweak focus parameters.
  3. Attach Scripts

    • Add CameraController.cs to your camera (handles orbit, pan, zoom).
    • Add AutoFocus.cs to the same GameObject.
    • Drag your Volume into the Volume field of AutoFocus.
  4. Play

    • Move or animate the focalPoint โ€” the camera focus updates automatically.

๐Ÿงฉ Public Variables

Variable Type Description
volume Volume Reference to the Global Volume containing Depth of Field
cameraController CameraController Camera control script providing the focal point

๐Ÿ’ก Example Scene

A demo scene could include:

  • A camera orbiting around a target sphere
  • A Global Volume with DoF enabled
  • AutoFocus attached to the same camera

๐Ÿ“œ License

MIT License โ€” feel free to use, modify, and share.


๐Ÿงท Related Repository

  • ๐ŸŽฎ CameraController: Scene-view-style orbit, pan, and zoom controller for Unityโ€™s new Input System.

About

A URP Unity script that dynamically adjusts camera Depth of Field in real time to keep the focal point perfectly in focus.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages