Search Unity

Resolved Raycast not hitting objects below certain Y value

Discussion in 'VR' started by gerasmussen, Jul 27, 2021.

  1. gerasmussen

    gerasmussen

    Joined:
    May 27, 2021
    Posts:
    32
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using System.Text.RegularExpressions;
    7.  
    8. public class MyLaser : MonoBehaviour
    9. {
    10.     private LineRenderer rend;
    11.     public Vector3 hitPoint;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         rend = gameObject.GetComponent<LineRenderer>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (OVRInput.Get(OVRInput.RawButton.LIndexTrigger) || OVRInput.Get(OVRInput.RawButton.Y))
    23.         {
    24.             StartLaser(transform.position, transform.forward, 10.0f);
    25.             rend.enabled = true;
    26.         }
    27.         else
    28.         {
    29.             rend.enabled = false;
    30.         }
    31.     }
    32.  
    33.     //looks at objects that raycast interacts with and adjusts rendered endPos when neccesary
    34.     void StartLaser(Vector3 targetPos, Vector3 direction, float length)
    35.     {
    36.         RaycastHit[] hits;
    37.         hits = Physics.RaycastAll(targetPos, direction * length);
    38.  
    39.         Vector3 endPos = targetPos + (direction * length);
    40.  
    41.         for (int i = 0; i < hits.Length; i++)
    42.         {
    43.             RaycastHit hit = hits[i];
    44.  
    45.             //if the ray hits/collides with something in the solid layer, a new end position is calculated to stop at said object
    46.             if (hit.collider.gameObject.layer == 11 || hit.collider.gameObject.layer == 5)
    47.             {
    48.                 endPos = hit.point;
    49.                 break;
    50.             }
    51.         }
    52.         hitPoint = endPos;
    53.  
    54.         //renders the line/laser
    55.         rend.SetPosition(0, targetPos);
    56.         rend.SetPosition(1, endPos);
    57.     }
    58. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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?
     
  3. gerasmussen

    gerasmussen

    Joined:
    May 27, 2021
    Posts:
    32
    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.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
    gerasmussen likes this.
  5. gerasmussen

    gerasmussen

    Joined:
    May 27, 2021
    Posts:
    32
    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!
     
    JoeStrout likes this.