Search Unity

PressInteraction and how to make it work

Discussion in 'Input System' started by hardcodednumber, Sep 15, 2019.

  1. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    Hi, I have to preface this by saying I really like the way the new Input System is turning out.

    I am trying to figure out how to get my button only to fire once per press. I found the interactions section in the Input Actions Editor. So I added the PressInteraction and saved the asset.

    However, in the callback of my action, it is still called called multiple times.

    Code (CSharp):
    1.        
    2.         public void OnJump(InputAction.CallbackContext context)
    3.         {
    4.             if (context.performed) {
    5.                 _jump = context.ReadValue<float>() != 0f;
    6.             }
    7.         }

    Here is my Input Actions

     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    This should work. What do you mean is called multiple times? OnJump? Or the part inside `if (context.performed) {`?
     
  3. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    The callback is still called multiple times, even when I add the conditional
     
    T-Zee and gilbertoPlayX like this.
  4. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Hmm, i have the same code here, and it works fine for me. You will need to submit a bug report with a repro project and post the case number here, so we can take a look.
     
  5. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    I figured out the problem, after attempting to re-create the project. It was not with the Input System, but user error. :D Press Iteraction is working as intended.

    This leads to another question. If this isn't the right forum, let me know. I want to know properly how to know if the player is jumping. I am having a problem to know when to properly set the _jump in my JobComponentSystem bool to false. Here is some context:

    I know when the user presses the jump button:
    Code (CSharp):
    1.  
    2.   public class CharacterControllerSystem : JobComponentSystem, IGameplayActions
    3.     {
    4.         private bool _jump;
    5.        ...
    6.         public void OnJump(InputAction.CallbackContext context)
    7.         {
    8.             _jump = context.ReadValue<float>() != 0f;
    9.         }
    10.      }
    I send the '_jump" same input to my job:

    Code (CSharp):
    1.  protected override JobHandle OnUpdate(JobHandle inputDependencies)
    2.         {
    3.             var phyicsJob = new CharacterControllerPhysicsJob {
    4.                ...
    5.             };
    6.  
    7.             inputDependencies = phyicsJob.Schedule(this, inputDependencies);
    8.             inputDependencies.Complete();
    9.  
    10.             var controllerDataType = GetArchetypeChunkComponentType<CharacterControllerData>();
    11.             var physicsColliderType = GetArchetypeChunkComponentType<PhysicsCollider>();
    12.             var physicsVelocityType = GetArchetypeChunkComponentType<PhysicsVelocity>();
    13.             var translationType = GetArchetypeChunkComponentType<Translation>(true);
    14.             var rotationType = GetArchetypeChunkComponentType<Rotation>();
    15.             var isGamepadConnected = Gamepad.current != null;
    16.  
    17.             var job = new CharacterControllerSystemJob {
    18.                 MoveDir = _move,
    19.                 Jump = _jump,
    20.                 ShootDir = _shootDir,
    21.                 DeltaTime = Time.deltaTime,
    22.                 GamepadConnected = isGamepadConnected,
    23.                 CharacterControllerType = controllerDataType,
    24.                 PhysicsColliderType = physicsColliderType,
    25.                 PhysicsVelocityType = physicsVelocityType,
    26.                 TranslationType = translationType,
    27.                 RotationType = rotationType
    28.             };
    29.  
    30.             return job.Schedule(_group, inputDependencies);
    31.         }
    In the Character Controller System Job, I check to see if the player is jumping. The character does jump. No matter what I do, I either see the player fly off or not move.

    Code (CSharp):
    1.              
    2.      if (characterData.IsJumping) {
    3.          linearVelocity.y += characterData.JumpSpeed;
    4.          characterData.IsJumping = false;
    5.          }