Auto Focus Unity

Sep 1st, 2014
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  4. public class FocusPoint : MonoBehaviour {
  5.     public GameObject focusObject;
  6.     public Camera camera;
  8.     void Update () {
  9.         if(camera==null||focusObject==null) return;
  10.         // Very basic, could add easing so it gradually changes focus, also should focus on a broader area than just a single point by casting multiple rays and averaging.
  11.         Vector3 fwd = camera.transform.TransformDirection(Vector3.forward);
  12.         RaycastHit hit;    
  14.         if (Physics.Raycast(camera.transform.position, fwd, out hit)){
  15.             Vector3 temp = new Vector3(0,0,hit.distance);
  16.             focusObject.transform.localPosition = temp;
  17.         }
  18.     }
  19. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.