0

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!

Danny
  • 11
  • 1
  • Currently I can't get it to switch weapons this way, so was wondering if anyone has come up with any solutions – Danny Apr 23 '18 at 23:51
  • What have you done to try and debug it yourself? – Draco18s no longer trusts SE Apr 24 '18 at 02:54
  • From the script, i've tried to change the objectToGrab with the current weapon. I know that the input is working on my Vive controller as i've done a debug log on it when pressed and that both of my weapons work as separate intractable objects. I've tried parenting a 'weapons holder' to the player, even tried to disable/enable different intractable scripts at runtime by setting them as unactive i'm still having no joy! – Danny Apr 24 '18 at 03:35
  • You're not digging deep enough. Is `SelectWeapon()` called? What value does `selectedWeapon` have? Does `weapon.gameObject.SetActive(true);` execute? You can't stop at "I know the input works," you have to figure out what line *isn't* working. THEN you have a question that can be answered. – Draco18s no longer trusts SE Apr 24 '18 at 06:32

0 Answers0