Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast hits everything even when pointed at the sky

Discussion in 'Scripting' started by JBEDUnity, Jul 31, 2020.

  1. JBEDUnity

    JBEDUnity

    Joined:
    Jul 13, 2020
    Posts:
    23
    No matter what I point at the raycast always comes back a "hit". Anyone know why?

    Code (CSharp):
    1. using Oculus.Platform.Models;
    2. using Oculus.Platform.Samples.VrHoops;
    3. using Pixyz.Utils;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.Linq;
    7. using UnityEngine;
    8.  
    9. public class Measure : MonoBehaviour
    10. {
    11.     //set up rays to represent hand to measurement point and point to point
    12.     public LineRenderer lrMeasure2;  //Ray from hand to point of measurement
    13.     //public LineRenderer Ray;    //Line showing point 1 to point 2
    14.  
    15.     //troubleshooting objects to determine if button pressed while in game
    16.     public GameObject[] objects;
    17.     private bool blActive;
    18.  
    19.     // Set up x,y,z to report on screen
    20.     private float flX;
    21.     private float flY;
    22.     private float flZ;
    23.  
    24.     //turn line on/off
    25.     private bool blLineActive =false;
    26.  
    27.     //get right hand position
    28.     public Transform trfmRightHand;
    29.  
    30.     // store input from right hand trigger
    31.     private bool blHandRight;//= OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger);
    32.  
    33.     // Start is called before the first frame update
    34.     void Start()
    35.     {
    36.         //Set up line render
    37.         Vector3[] v3StartLinePosition = new Vector3[2] { Vector3.zero, new Vector3(0,0,100)};
    38.         //v3StartLinePosition[1].Scale(Vector3.forward, new Vector3(0,0,100));
    39.         lrMeasure2.SetPositions(v3StartLinePosition);
    40.         lrMeasure2.enabled = false;
    41.     }
    42.  
    43.     // Update is called once per frame
    44.     void Update()
    45.     {
    46.         //string strTemp;
    47.  
    48.         //Run routine if right trigger button is pressed
    49.         blHandRight = OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger);
    50.  
    51.         if (blHandRight)
    52.         {
    53.             blLineActive = true;
    54.             lrMeasure2.enabled = true;
    55.             Debug.Log("Right Trigger Pressed");
    56.         } else
    57.         {
    58.             lrMeasure2.enabled = false;
    59.             blLineActive = false;          
    60.         }
    61.  
    62.         if (blLineActive)
    63.         {
    64.             //get point data
    65.             RaycastMeasure();
    66.  
    67.             for (int i = 0; i < objects.Length; i++)
    68.             {
    69.  
    70.                 blActive = !objects[i].activeSelf;
    71.                 objects[i].SetActive(blActive);
    72.  
    73.             }
    74.         }
    75.     }
    76.  
    77.     private void RaycastMeasure()
    78.     {
    79.         RaycastHit rchHit;
    80.  
    81.         //get position of hit on the mesh
    82.         if (Physics.Raycast(trfmRightHand.position, trfmRightHand.forward, out rchHit))
    83.         {
    84.                 flX = rchHit.point.x;
    85.                 flY = rchHit.point.y;
    86.                 flZ = rchHit.point.z;
    87.  
    88.                 Debug.Log(flX.ToString()+ ", "+flY.ToString()+", "+flX.ToString());
    89.  
    90.         }
    91.     }
    92. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Print out the name of what it's hitting.
     
    PraetorBlue and adamgolden like this.
  3. clinkr

    clinkr

    Joined:
    Mar 20, 2020
    Posts:
    3
    Add a range to the raycast arguments
     
  4. JBEDUnity

    JBEDUnity

    Joined:
    Jul 13, 2020
    Posts:
    23
    Thanks. Turns out I'm hitting the object it's attached to. Any ideas how to fix that?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    A couple main ways:
    1. Make sure the ray starts outside of any colliders of the source object
    2. Use a layermask which exclude the source object's layer.
     
    Kurt-Dekker and Vryken like this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Debug.Log() wins again!

    As Praetor above noted, two good reasons. Let me add this:

    3. Disable the collider on the thing you're casting from, then flip it back on afterwards.
     
  7. JBEDUnity

    JBEDUnity

    Joined:
    Jul 13, 2020
    Posts:
    23
    Got the layermask method working. Thanks!
     
    Kurt-Dekker likes this.