Search Unity

Question cant get input to work

Discussion in 'XR Interaction Toolkit and Input' started by VRGamer1234, Apr 26, 2021.

  1. VRGamer1234

    VRGamer1234

    Joined:
    Nov 14, 2020
    Posts:
    4
    so I'm trying to make a gun in unity and i want it so if i push a button on the controller it will drop the mag. i cant seem to figure out a way to detect input. can i get some help with getting input
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.XR;
    6. using UnityEngine.XR.Interaction.Toolkit;
    7.  
    8. public class gunmagholder : XRSocketInteractor
    9. {
    10.  
    11.     public parentgrabbable gun;
    12.     private parentgrabbable mag;
    13.     private XRBaseInteractable Magezene;
    14.     public gunmagholder script;
    15.     private Rigidbody rb;
    16.     private XRController controller;
    17.     private bool getGameobj;
    18.     public bool cangrabout;
    19.     public Text debugger;
    20.  
    21.     protected override void OnSelectEntered(XRBaseInteractable interactable)
    22.     {
    23.         mag = interactable.GetComponent<parentgrabbable>();
    24.         rb = interactable.GetComponent<Rigidbody>();
    25.         Magezene = interactable;
    26.         if (mag != null && cangrabout == false)
    27.         {
    28.             mag.enabled = false;
    29.         }
    30.     }
    31.  
    32.  
    33.     protected override void Start()
    34.     {
    35.         debugger.text = "test";
    36.     }
    37.  
    38.  
    39.  
    40.     void Update()
    41.     {
    42.         if (gun.isSelected && cangrabout == false) {
    43.             if (controller.inputDevice.TryGetFeatureValue(CommonUsages.secondaryButton, out bool button))
    44.             {
    45.                 magdrop();
    46.                 debugger.text = "button pressed";
    47.             }
    48.         }
    49.  
    50.         if (gun.isSelected && getGameobj == false)
    51.         {
    52.             getGameobj = true;
    53.             controller = gun.selectingInteractor.GetComponent<XRController>();
    54.             debugger.text = "got controller";
    55.         }
    56.         else if (gun.isSelected == false)
    57.         {
    58.             getGameobj = false;
    59.             controller = null;
    60.         }
    61.     }
    62.  
    63.  
    64.     void magdrop()
    65.     {
    66.         script.allowSelect = false;
    67.         mag.enabled = true;
    68.         rb.AddForce(0, -1, 0);
    69.         Invoke("EndMagDrop", 3);
    70.     }
    71.  
    72.     void EndMagDrop()
    73.     {
    74.         script.allowSelect = true;
    75.         rb = null;
    76.         mag = null;
    77.         Magezene = null;
    78.     }
    79. }
    80.