Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Continuous Pointer with Physics RayCasting within AR Scene

Discussion in 'AR' started by mandivision, Dec 4, 2019.

  1. mandivision

    mandivision

    Joined:
    Mar 17, 2019
    Posts:
    8
    I'm trying to created a continuous raycast from the center of the viewport that triggers a script on the other object it hits (to activate UI and a few other functions. The idea is that there are lots of objects in the AR scene and the user reveals UI/information about them by pointing their camera towards it. I'm essentially trying to achieve this effect but within an AR scene:


    I have the script working on a Desktop Build with an FPS Controller and have it working with AR Foundation if the raycast is triggered by touching the screen, but anytime I try to have it work without input from the user other than moving it around I get this error in the debug:
    NullReferenceException: Object reference not set to an instance of an object.

    Here's my code (color Change is the script on the object that activates the UI and other "selected" functions):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class raycastSelectionAR : MonoBehaviour
    6. {   [SerializeField] private Color highlightMaterial;
    7.     [SerializeField] private Color defaultMaterial;
    8.     [SerializeField] private string selectableTag= "Subjects";
    9.     [SerializeField] private Camera cam;
    10.     //public GameObject UIElements;
    11.     void Awake(){
    12.         cam = GetComponent<Camera>();
    13.     }
    14.     void Update()
    15.     {
    16.         var ray =cam.ViewportPointToRay(new Vector3(0.5f,0.5f,0));
    17.         RaycastHit hit;
    18.         if(Physics.Raycast(ray, out hit))
    19.         {
    20.             var selection = hit.transform;
    21.             if (selection.CompareTag(selectableTag))
    22.             {  
    23.                 Debug.Log("cast has hit a profile graphic!");
    24.                 Debug.Log(hit.collider.gameObject.name);
    25.              
    26.                 selection.gameObject.GetComponent<colorChange>().hitByRay = true;
    27.                
    28.             }
    29.         }
    30.     }
    31. }


    Any help greatly appreciated!
     
  2. kpatulrpatel

    kpatulrpatel

    Joined:
    Mar 13, 2017
    Posts:
    7
    —> In Editor Testing
    // debug Ray
    Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.red);

    —>In Game View used line renderer
     
  3. kpatulrpatel

    kpatulrpatel

    Joined:
    Mar 13, 2017
    Posts:
    7
    Add Condition:

    If(hit.collider != null)
    Debug.Log(hit.collider.gameobject.name);