Search Unity

Problems with Raycasting in Unity 5

Discussion in 'Scripting' started by keenanwoodall, Mar 5, 2015.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    I'm trying to create a simple script that shoots out a raycast and then moves Unitys DeothOfField, image effect scripts, focal point to the raycasts hit point. Whenever the ray hits something, I get an error.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityStandardAssets.ImageEffects;
    4. using System.Collections;
    5.  
    6. public class DOFFocusFinder : MonoBehaviour
    7. {
    8.     public float maxDistance = 100f;
    9.     public Vector3 defaultFocusPoint = new Vector3(0f, 0f, 10f);
    10.     public float adjustSpeed = 2f;
    11.     public DepthOfField depthOfField;
    12.  
    13.     private Ray ray = new Ray();
    14.     private RaycastHit hit = new RaycastHit();
    15.     private Transform focusPoint;
    16.  
    17.     void Update ()
    18.     {
    19.         if(Physics.Raycast(transform.position, transform.forward, out hit, maxDistance))
    20.             //The error is a null-reference and it directs me to this line
    21.             focusPoint.position = Vector3.Lerp(focusPoint.position, hit.point, adjustSpeed * Time.deltaTime);
    22.         else
    23.             //and also this line, when the raycast doesn't return true
    24.             focusPoint.position = Vector3.Lerp(focusPoint.position, transform.position + defaultFocusPoint, adjustSpeed * Time.deltaTime);
    25.  
    26.         depthOfField.focalTransform = focusPoint;
    27.     }
    28. }
     
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    "focusPoint" was never set?
     
  3. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    245
    Stick this on line 20:

    Debug.Log(focusPoint);
    Debug.Log(hit);
     
  4. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    Good call, here's the working code. Basically, just make an empty gameobject and drag it into the focalTransform slot in the DOF script and also drag it into the focusTransform slot on the DOFFocusFinder script.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityStandardAssets.ImageEffects;
    4. using System.Collections;
    5.  
    6. public class DOFFocusFinder : MonoBehaviour
    7. {
    8.     public float maxDistance = 100f;
    9.     public Vector3 defaultFocusPoint = new Vector3(0f, 0f, 10f);
    10.     public float adjustSpeed = 2f;
    11.     public Transform focusTransform;
    12.     public DepthOfField depthOfField;
    13.  
    14.     private Ray ray = new Ray();
    15.     private RaycastHit hit = new RaycastHit();
    16.  
    17.     void Start()
    18.     {
    19.         focusTransform.position = transform.position + (transform.rotation * defaultFocusPoint);
    20.     }
    21.     void Update ()
    22.     {
    23.         if(Physics.Raycast(transform.position, transform.forward, out hit, maxDistance))
    24.             focusTransform.position = Vector3.Lerp(focusTransform.position, hit.point, adjustSpeed * Time.deltaTime);
    25.         else
    26.             focusTransform.position = Vector3.Lerp(focusTransform.position, transform.position + (transform.rotation * defaultFocusPoint), adjustSpeed * Time.deltaTime);
    27.     }
    28. }