Raycast hits everything even when pointed at the sky

No matter what I point at the raycast always comes back a “hit”. Anyone know why?

using Oculus.Platform.Models;
using Oculus.Platform.Samples.VrHoops;
using Pixyz.Utils;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Measure : MonoBehaviour
{
    //set up rays to represent hand to measurement point and point to point
    public LineRenderer lrMeasure2;  //Ray from hand to point of measurement
    //public LineRenderer Ray;    //Line showing point 1 to point 2

    //troubleshooting objects to determine if button pressed while in game
    public GameObject[] objects;
    private bool blActive;

    // Set up x,y,z to report on screen
    private float flX;
    private float flY;
    private float flZ;

    //turn line on/off
    private bool blLineActive =false;

    //get right hand position
    public Transform trfmRightHand;

    // store input from right hand trigger
    private bool blHandRight;//= OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger);

    // Start is called before the first frame update
    void Start()
    {
        //Set up line render
        Vector3[] v3StartLinePosition = new Vector3[2] { Vector3.zero, new Vector3(0,0,100)};
        //v3StartLinePosition[1].Scale(Vector3.forward, new Vector3(0,0,100));
        lrMeasure2.SetPositions(v3StartLinePosition);
        lrMeasure2.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        //string strTemp;

        //Run routine if right trigger button is pressed
        blHandRight = OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger);

        if (blHandRight)
        {
            blLineActive = true;
            lrMeasure2.enabled = true;
            Debug.Log("Right Trigger Pressed");
        } else
        {
            lrMeasure2.enabled = false;
            blLineActive = false;           
        }

        if (blLineActive)
        {
            //get point data
            RaycastMeasure();

            for (int i = 0; i < objects.Length; i++)
            {

                blActive = !objects[i].activeSelf;
                objects[i].SetActive(blActive);

            }
        }
    }

    private void RaycastMeasure()
    {
        RaycastHit rchHit;

        //get position of hit on the mesh
        if (Physics.Raycast(trfmRightHand.position, trfmRightHand.forward, out rchHit))
        {
                flX = rchHit.point.x;
                flY = rchHit.point.y;
                flZ = rchHit.point.z;

                Debug.Log(flX.ToString()+ ", "+flY.ToString()+", "+flX.ToString());

        }
    }
}

Print out the name of what it’s hitting.

2 Likes

Add a range to the raycast arguments

Thanks. Turns out I’m hitting the object it’s attached to. Any ideas how to fix that?

A couple main ways:

  • Make sure the ray starts outside of any colliders of the source object
  • Use a layermask which exclude the source object’s layer.
2 Likes

Debug.Log() wins again!

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

  1. Disable the collider on the thing you’re casting from, then flip it back on afterwards.

Got the layermask method working. Thanks!

1 Like