Search Unity

Trying to get a raycast to give info when hitting an object

Discussion in 'AR/VR (XR) Discussion' started by JBEDUnity, Jul 29, 2020.

  1. JBEDUnity

    JBEDUnity

    Joined:
    Jul 13, 2020
    Posts:
    23
    So I added this code to a CustomHandRight.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Measure : MonoBehaviour
    6. {
    7.     public GameObject[] objects;
    8.     public bool blActive;
    9.     public Transform trfmRightHand;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         //string strTemp;
    21.         if (OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger))
    22.         {
    23.             RaycastMeasure();
    24.  
    25.         }
    26.     }
    27.  
    28.     private void RaycastMeasure()
    29.     {
    30.         RaycastHit hit;
    31.         //string strTemp;
    32.         if (Physics.Raycast(trfmRightHand.position, trfmRightHand.forward, out hit))
    33.         {
    34.             if (hit.collider.gameObject.CompareTag("Stack"))
    35.             {
    36.                 for (int i = 0; i < objects.Length; i++)
    37.                 {
    38.  
    39.                     blActive = !objects[i].activeSelf;
    40.  
    41.                     Debug.Log("Hit");
    42.                     objects[i].SetActive(blActive);
    43.  
    44.                 }
    45.             }
    46.  
    47.  
    48.         }
    49.  
    50.  
    51.     }
    52.  
    53.  
    54.  
    55. }
    The only reason I'm turning objects on/off is cause it's easier to see if its working (haven't been able to figure out how to get the line renderer ray working yet). I'm ultimately trying to make a measurement tool where I can click from one object to another but for the time being I can only get the raycast to run each click. It wont' work when I try to read if the ray actually hit something (for now I added a tag). Any advice?