Unity virtual reality weapon switch (VRTK)

147 Views Asked by At

I currently have a game with 2 weapons, instead of running around and picking up the weapons, I would like the player to be able to switch between them.

I have a basic weapon selector script:

public VRTK.VRTK_ObjectAutoGrab autoGrab;
 public VRTK.VRTK_ControllerEvents controllerEvents;
 public VRTK.VRTK_InteractableObject[] weapons;

 public int selectedWeapon = 0;

 void Start () {


     SelectWeapon();
 }

 void Update () {

     int previousSelectedWeapon = selectedWeapon;

     if(controllerEvents.gripClicked)
     {
         if(selectedWeapon >= transform.childCount - 1)
         {
             selectedWeapon = 0;
         }
         selectedWeapon++;
     }

     if(previousSelectedWeapon != selectedWeapon)
     {
         SelectWeapon();
     }

 }

 void SelectWeapon()
 {
     int i = 0;

     foreach(Transform weapon in transform)
     {

         if(i == selectedWeapon)
         {
             //autoGrab.objectToGrab = weapons[i];
             weapon.gameObject.SetActive(true);

         }
         else
         {
             weapon.gameObject.SetActive(false);
         }

         i++;
     }
 }

In this script I am trying to use the grip buttons to change the weapons by changing the autograb objecttograb(changing the weapon). I'm not sure if this is the correct way of doing this or if this is possible, but any help is appreciated!

0

There are 0 best solutions below