Search Unity

Use case: How to handle weapon switching?

Discussion in 'Input System' started by Saucyminator, Nov 21, 2019.

  1. Saucyminator

    Saucyminator

    Joined:
    Nov 23, 2015
    Posts:
    61
    Hi all.

    I'm having some trouble with switching weapons in what I'm currently developing (a weapon system). What's a good way to handle input using the new input system for weapon switching?

    I have a WeaponManager-script that creates the weapons under an empty gameobject as weapon-parent. And each weapon has a PlayerInput-script and a Weapon-script attached to it. I'm using Invoke Unity Events behavior on the PlayerInput so I can hook up different scripts on it to be triggered.

    But when I start a game with multiple weapons created my inputs are sometimes double or even more. Even if the weapons are set to disabled after creation. After switching weapons the inputs are are often triggered even more times. Even when I have code that checks if input was performed.

    Weapon.cs has this:
    Code (CSharp):
    1.  
    2.     public PlayerInput PlayerInput;
    3.  
    4.     private void OnEnable () {
    5.       PlayerInput.ActivateInput();
    6.     }
    7.  
    8.     private void OnDisable () {
    9.       PlayerInput.PassivateInput();
    10.     }
    Which per the documentation should enable/disable the inputs on the gameobject.

    Here's a gif with only one weapon created (one casing):
    https://i.imgur.com/uw5VV7H.mp4

    and in this case I added another weapon to be created when I start the game (two casings, second gameobject is disabled):
    https://i.imgur.com/Sjx9yB1.mp4
     
  2. Jichaels

    Jichaels

    Joined:
    Dec 27, 2018
    Posts:
    237
    PlayerInput should handle all inputs from your PLAYER not your weapon, your weapon logic should not be linked to inputs directly
     
  3. Saucyminator

    Saucyminator

    Joined:
    Nov 23, 2015
    Posts:
    61
    Alright. Thanks, I was worried that was the case. Back to the drawing board for me.