Advertisement
vjanomolee

CardReader.cs

Jan 31st, 2023
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.Interaction.Toolkit;
  5.  
  6. public class CardReader : XRSocketInteractor
  7. {
  8.   private bool m_SwipeIsValid = false;
  9.   private Vector3 m_HoverEntry;
  10.   public float AllowedUprightErrorRange = 0.1f;
  11.   public GameObject visualLockToHide;
  12.  
  13.   //public bool handleToEnable ; // Handle this after you complete the CardReader
  14.   public float minDisThroughReader = -0.15f;
  15.   private KeyDetector keyDetector;
  16.  
  17.   public GameObject m_KeycardGameObject;
  18.   public Transform m_KeycardTransform;
  19.  
  20.   protected override void OnHoverEntered(HoverEnterEventArgs args)
  21.   {
  22.     base.OnHoverEntered(args);
  23.     keyDetector.iKeycard = m_KeycardGameObject;  //  Can't seem to figure out how to reference an instantiated GameObject(iKeycard)
  24.                                                   // from the KeyDetector script????  
  25.     m_KeycardTransform = m_KeycardGameObject.transform.position;
  26.     m_KeycardTransform = args.interactableObject.transform;
  27.     m_HoverEntry = m_KeycardTransform.position;
  28.     m_SwipeIsValid = true;
  29.     Debug.Log("Hover has been entered");
  30.   }
  31.  
  32.   protected override void OnHoverExited(HoverExitEventArgs args)
  33.   {
  34.     base.OnHoverExited(args);
  35.  
  36.     Vector3 entryToExit = m_KeycardTransform.position - m_HoverEntry;
  37.  
  38.     if (m_SwipeIsValid && entryToExit.y < minDisThroughReader)
  39.     {
  40.       visualLockToHide.gameObject.SetActive(false);
  41.       //handleToEnable.enabled = true; // // Handle this after you complete the CardReader
  42.     }
  43.  
  44.     m_KeycardTransform = null;
  45.   }
  46.  
  47.   private void Update()
  48.   {
  49.     if (m_KeycardTransform != null)
  50.     {
  51.       Vector3 keycardUp = m_KeycardTransform.forward;
  52.       float dot = Vector3.Dot(keycardUp, Vector3.up);
  53.  
  54.       if (dot < 1 - AllowedUprightErrorRange)
  55.       {
  56.         m_SwipeIsValid = false;
  57.       }
  58.     }
  59.   }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement