Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JoaoReb · Apr 28, 2016 at 05:19 AM · raycastarraystriggerscomparison

Problem with 2+ of the same scripts on the same object and functions

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Apr 28, 2016 at 06:05 AM

interactible will never be the same as m_lastInteractible. You are comparing arrays, not the content of the array.

   if (interactible != null && interactible != m_LastInteractible)

the second check compares the content of the two references, but not the items in the array. To check the items, you'd have to compare address, the compare length and finally iterate each item of A to be found in B.

Each round of raycast you assign a new array to interactible, again this is the reason why the comparison does not work, tho it may contain the same items as previous frame, the comparison compares the address and this is a new array at a new address.

I would assume it would be enough to use a game object reference since you are not colliding many objects:

  if (Physics.Raycast (ray, out hit, m_RayLength, ~m_ExclusionLayers)) {
          GameObject interactible =  hit.collider.gameObject; // CHANGE HERE
          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_LastInteractible and interactible are now GameObject type.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoaoReb · Apr 28, 2016 at 03:36 PM 1
Share

I had to change the code a bit to make it work since it was giving me a error, but aside from that, its working perfectly. Thanks for the help. Here's what i had to add in case someone wants to know:

     if (Physics.Raycast (ray, out hit, m_RayLength, ~m_ExclusionLayers)) {
                 GameObject interactible = hit.collider.gameObject; //attempt to get the VRInteractiveItem on the hit object
 
                 Trigger [] triggers = interactible.GetComponents<Trigger>(); //Added this
     
                 // 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 triggers)
                         trigger.LookIn ();

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast Arrays 1 Answer

array and 2d array comparison in javascript problem 2 Answers

How can I Raycast to multiple objects 1 Answer

Comparing tags stored in array with the tag of GameObject (Raycasting) 1 Answer

How to get raycast only detect first object from the same masked layer.. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges