Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Weapon Charge with Unity's New Input System

Discussion in 'Input System' started by Will_at_BridgeWorxGames, Sep 3, 2020.

  1. Will_at_BridgeWorxGames

    Will_at_BridgeWorxGames

    Joined:
    Feb 20, 2020
    Posts:
    12
    Hey all,

    Unity 2020.1.f1Personal

    I want my avatar to be able to press a single fire button for a normal weaker/quicker shot, or hold the same button for a more powerful shot. I want the player to hold the button for 2 seconds to make that charged shot.

    The unity doc for the input system makes this exact same setup here. So that's cool. It works! yay! But with the settings that come default, the charge happens only a fraction of a second after the normal fire, when I'm looking for more like holding (or a slow tap) for 2 seconds.

    No matter how I fudge with the various durations, I cannot find the magic combination to get the results I'm looking for.

    How what durations should I set for the tap or slow tap interactions? What type of Action should I choose for Property of the ... action? "Value" with "any"? Button?

    Here some screenshots of what I've tried:
    upload_2020-9-2_18-19-55.png

    Slightly slower charging ... maybe? I mean, if so we're talking a fraction of a second.
    upload_2020-9-2_18-20-10.png

    This won't ever reach the "charged" interaction, but only the "canceled" interaction.
    upload_2020-9-2_18-24-10.png

    Settings from the doc with a super duper fast charge.
    upload_2020-9-2_18-21-57.png
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Why not just track how long the button is pressed on a script instead of hobbling press restrictions into the input sources?
     
  3. Will_at_BridgeWorxGames

    Will_at_BridgeWorxGames

    Joined:
    Feb 20, 2020
    Posts:
    12
    It was an issue with my script, I messed up on the copy / pasta. I had two "started" functions in my script, which is why edited the value of the durations seemed like they did anything.

    Editing the code properly fixed everything.

    I went from this:
    Code (CSharp):
    1. controls.Gameplay.Fire.started += ctx =>
    2.         {
    3.             if (ctx.interaction is SlowTapInteraction)
    4.                 Debug.Log("Charging...");
    5.         };
    6.         controls.Gameplay.Fire.[B]started[/B] += ctx =>
    7.         {
    8.             if (ctx.interaction is SlowTapInteraction)
    9.                 Debug.Log("Charged fire");
    10.             else
    11.                 Debug.Log("Normal fire");
    12.         };
    13.         controls.Gameplay.Fire.canceled += ctx =>
    14.         {
    15.             Debug.Log("canceled.");
    16.  
    17.         };
    To this:
    Code (CSharp):
    1.         controls.Gameplay.Fire.started += ctx =>
    2.         {
    3.             if (ctx.interaction is SlowTapInteraction)
    4.                 Debug.Log("Charging...");
    5.         };
    6.         controls.Gameplay.Fire.[B]performed[/B] += ctx =>
    7.         {
    8.             if (ctx.interaction is SlowTapInteraction)
    9.                 Debug.Log("Charged fire");
    10.             else
    11.                 Debug.Log("Normal fire");
    12.         };
    13.         controls.Gameplay.Fire.canceled += ctx =>
    14.         {
    15.             Debug.Log("canceled.");
    16.  
    17.         };