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);
}
}