Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

HTCvive input utility How do I detect a raycast on object?

Discussion in 'Scripting' started by Unlimited_Energy, Jan 27, 2019.

  1. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    I need an object in the environment to be able to detect it has a ray cast at it. I have onmousedown methods that need to be replaced with a VRLaser pointer trigger pull as a left mouse button on the object. I am having great dificulty trying to figure out if the object is being hit with the raycast. I need this code to be in a script on the object benign hit and not on the vivewand detecting if an object is in its ray. Im using the htcvive input utility.
     
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    Hey I used those 2 scripts the first one is for the hand the second one for the objects you want to interact with.

    Code (CSharp):
    1. public class VrHandRayCaster : MonoBehaviour
    2. {
    3.     public LayerMask handRayLayer;
    4.     public UnityEngine.XR.XRNode handVrNode;
    5.     public float rayLength;
    6.     public LineRenderer lineRenderer;
    7.     private bool isHovering;
    8.     private GameObject currentGameObject;
    9.     private int currentObjectID = 123456701;
    10.     private Interactive currentInteractive;
    11.     void Start ()
    12.     {
    13.        
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         Ray ray = new Ray(this.transform.position, this.transform.forward);
    20.         RaycastHit hit;
    21.         if (Physics.Raycast(ray, out hit, rayLength, handRayLayer))
    22.         {
    23.             int objectID = hit.transform.gameObject.GetInstanceID();
    24.             if(currentObjectID != objectID)
    25.             {
    26.                 if(currentGameObject != null)
    27.                     currentInteractive.IsHovering = false;
    28.                 currentObjectID = objectID;
    29.                 currentGameObject = hit.transform.gameObject;
    30.                 isHovering = true;
    31.                 currentInteractive = currentGameObject.transform.GetComponent<Interactive>();
    32.             }
    33.             else
    34.             {
    35.                 if(!currentInteractive.IsInUse)
    36.                 {  
    37.                     currentInteractive.IsHovering = true;
    38.                     isHovering = true;
    39.                     if(Input.GetAxis("MainTriggerRight") > 0.9f && handVrNode == UnityEngine.XR.XRNode.RightHand || Input.GetAxis("MainTriggerLeft") > 0.9f && handVrNode == UnityEngine.XR.XRNode.LeftHand)
    40.                             currentInteractive.WasPressed = true;
    41.                 }
    42.              }
    43.             lineRenderer.enabled = true;
    44.             lineRenderer.SetPosition(1,new Vector3(0,0,hit.distance));
    45.             return;
    46.          }
    47.         else
    48.         {
    49.             if(isHovering)
    50.             {
    51.                 currentObjectID = 123456701;
    52.                 lineRenderer.enabled = false;
    53.                 currentInteractive.IsHovering = false;
    54.                 isHovering = false;
    55.             }
    56.         }
    57.     }
    58.  
    59.     public bool IsHovering {
    60.         get {
    61.             return isHovering;
    62.         }
    63.         set {
    64.             isHovering = value;
    65.         }
    66.     }
    67. }
    68.  
    Code (CSharp):
    1. public class Interactive : MonoBehaviour
    2. {
    3.     private bool isHovering = false;
    4.     private bool wasPressed = false;
    5.     private bool isInUse = false;
    6.  
    7.     public bool WasPressed
    8.     {
    9.         get {
    10.             return wasPressed;
    11.         }
    12.         set {
    13.             wasPressed = value;
    14.         }
    15.     }
    16.  
    17.     public bool IsHovering
    18.     {
    19.         get {
    20.             return isHovering;
    21.         }
    22.         set {
    23.             isHovering = value;
    24.         }
    25.     }
    26.  
    27.     public bool IsInUse {
    28.         get {
    29.             return isInUse;
    30.         }
    31.         set {
    32.             isInUse = value;
    33.         }
    34.     }
    35. }
    36.