HeliosDoubleSix

Auto Focus Unity

Sep 1st, 2014
426
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class FocusPoint : MonoBehaviour {
  5.     public GameObject focusObject;
  6.     public Camera camera;
  7.  
  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;    
  13.  
  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