Raycast not hitting objects below certain Y value

I am creating a template for the Oculus Quest 2. This template is to be used for future VR projects that are to be developed on the Quest. It includes controls and features such as teleporting, lasers, grabbing objects, etc. My lasers are supposed to be shot by clicking/holding a certain button and the rendering is supposed to be recalculated and stop when the raycast hits objects of specific layers. However, this rendering goes through objects in these layers if I move them downwards and shine the laser again. Not sure if this is an issue in my script, editor settings, etc.
Attached is my laser script. Any help is appreciated.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;

public class MyLaser : MonoBehaviour
{
    private LineRenderer rend;
    public Vector3 hitPoint;

    // Start is called before the first frame update
    void Start()
    {
        rend = gameObject.GetComponent<LineRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (OVRInput.Get(OVRInput.RawButton.LIndexTrigger) || OVRInput.Get(OVRInput.RawButton.Y))
        {
            StartLaser(transform.position, transform.forward, 10.0f);
            rend.enabled = true;
        }
        else
        {
            rend.enabled = false;
        }
    }

    //looks at objects that raycast interacts with and adjusts rendered endPos when neccesary
    void StartLaser(Vector3 targetPos, Vector3 direction, float length)
    {
        RaycastHit[] hits;
        hits = Physics.RaycastAll(targetPos, direction * length);

        Vector3 endPos = targetPos + (direction * length);

        for (int i = 0; i < hits.Length; i++)
        {
            RaycastHit hit = hits[i];

            //if the ray hits/collides with something in the solid layer, a new end position is calculated to stop at said object
            if (hit.collider.gameObject.layer == 11 || hit.collider.gameObject.layer == 5)
            {
                endPos = hit.point;
                break;
            }
        }
        hitPoint = endPos;

        //renders the line/laser
        rend.SetPosition(0, targetPos);
        rend.SetPosition(1, endPos);
    }
}

I don’t quite understand what you’re trying to do, but I note that you’ve got your ray set up to only extend 10 meters. Could that be the problem?

I don’t believe so but I can mess with it.

Basically when an object I am shooting a laser at is above a certain Y value everything works correctly where the laser is not rendered past the object. However, when it has a smaller Y value/ is closer to the ground level the raycast must not be detecting the objects as you can see the laser coming out the other end.

Hmm. Your code looks correct. In such situations I usually start just logging the heck out of everything… log all the raycast hits, and see if that gives you a clue.

1 Like

It seems that the break I included in line 49 was not necessary and messing up my preferred laser function. I believe that it was always retrieving the farthest hit. So when the laser was below the plane horizon it was calling the plane instead of the object I wanted. However, when the sky was what was beyond my object it worked fine because that was the last solid object along the raycast.
Thank you for helping me work through it!

1 Like