I’m trying to adapt unity’s VR interactivity scripts (VREyeRaycaster.cs) to trigger events. I already have a fully functioning script that makes it so you select a type of trigger (collision, etc) and a type of event on a specific object. That script has 2 triggers, “LookIn” and “LookOut”, meaning, i need to add 2 or more of the trigger.cs to the same gameObjects. The VR script uses raycasts to determine if you’re looking or not.
I know i can use GetComponents to get an array, but my problem is the fact that theres 2 variables, ‘interactible’ which is the trigger components of the collided raycast, and ‘m_LastInteractible’, which is the array of the last interactible.
For some reason its always triggering the “LookIn” and “LookOut” function, even tho i only want the moment it collides and stops colliding.
The Code in question is here:
using System;
using UnityEngine;
// In order to interact with objects in the scene
// this class casts a ray into the scene and if it finds
// a VRInteractiveItem it exposes it for other classes to use.
// This script should be generally be placed on the camera.
public class VREyeRaycaster : MonoBehaviour
{
public event Action<RaycastHit> OnRaycasthit; // This event is called every frame that the user's gaze is over a collider.
[SerializeField] private Transform m_Camera;
[SerializeField] private LayerMask m_ExclusionLayers; // Layers to exclude from the raycast.
[SerializeField] private bool m_ShowDebugRay; // Optionally show the debug ray.
[SerializeField] private float m_DebugRayLength = 5f; // Debug ray length.
[SerializeField] private float m_DebugRayDuration = 1f; // How long the Debug ray will remain visible.
[SerializeField] private float m_RayLength = 500f; // How far into the scene the ray is cast.
//public Trigger m_CurrentInteractible; //The current interactive item
public Trigger [] m_LastInteractible; //The last interactive item
// Utility for other classes to get the current interactive item
/*public Trigger CurrentInteractible
{
get { return m_CurrentInteractible; }
}*/
private void Update()
{
EyeRaycast();
}
private void EyeRaycast()
{
// Show the debug ray if required
if (m_ShowDebugRay)
{
Debug.DrawRay(m_Camera.position, m_Camera.forward * m_DebugRayLength, Color.blue, m_DebugRayDuration);
}
// Create a ray that points forwards from the camera.
Ray ray = new Ray(m_Camera.position, m_Camera.forward);
RaycastHit hit;
// Do the raycast forweards to see if we hit an interactive item
if (Physics.Raycast (ray, out hit, m_RayLength, ~m_ExclusionLayers)) {
Trigger [] interactible = hit.collider.GetComponents<Trigger> (); //attempt to get the VRInteractiveItem on the hit object
//m_CurrentInteractible = interactible;
// If we hit an interactive item and it's not the same as the last interactive item, then call Over
if (interactible != null && interactible != m_LastInteractible)
foreach(Trigger trigger in interactible)
trigger.LookIn ();
// Deactive the last interactive item
if (interactible != m_LastInteractible)
DeactiveLastInteractible ();
m_LastInteractible = interactible;
if (OnRaycasthit != null)
OnRaycasthit (hit);
} else {
// Nothing was hit, deactive the last interactive item.
DeactiveLastInteractible ();
//m_CurrentInteractible = null;
}
}
private void DeactiveLastInteractible()
{
if (m_LastInteractible == null)
return;
foreach(Trigger trigger in m_LastInteractible)
trigger.LookOut ();
m_LastInteractible = null;
}
}