Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug TapInteraction is working abnormally on Gamepad

Discussion in 'Input System' started by Ice_106, Jan 17, 2024.

  1. Ice_106

    Ice_106

    Joined:
    Jun 17, 2019
    Posts:
    5
    The Process() method in TapInteraction seems to be incorrect.
    Code (CSharp):
    1.         public void Process(ref InputInteractionContext context)
    2.         {
    3.             if (context.timerHasExpired)
    4.             {
    5.                 context.Canceled();
    6.                 return;
    7.             }
    8.  
    9.             if (context.isWaiting && context.ControlIsActuated(pressPointOrDefault))
    10.             {
    11.                 m_TapStartTime = context.time;
    12.                 // Set timeout slightly after duration so that if tap comes in exactly at the expiration
    13.                 // time, it still counts as a valid tap.
    14.                 context.Started();
    15.                 context.SetTimeout(durationOrDefault + 0.00001f);
    16.                 return;
    17.             }
    18.  
    19.             if (context.isStarted && !context.ControlIsActuated(releasePointOrDefault))
    20.             {
    21.                 if (context.time - m_TapStartTime <= durationOrDefault)
    22.                 {
    23.                     context.Performed();
    24.                 }
    25.                 else
    26.                 {
    27.                     ////REVIEW: does it matter to cancel right after expiration of 'duration' or is it enough to cancel on button up like here?
    28.                     context.Canceled();
    29.                 }
    30.             }
    31.         }
    If you use the trigger key or other continuous input buttons and keep holding them down (beyond pressPointOrDefault), after timeout, they will return to the isWaiting state due to cancellation. At this point, release the button because the release process is continuous. Started() will be executed again and then Performed() will be executed because it is below releasePointOrDefault.The timeout limit is therefore invalid.
    I think it is reasonable to not trigger interaction before releasing this button after a timeout. So I modified the code like this, and then correctly triggered the interaction on the trigger key.
    Code (CSharp):
    1.         bool waitUntilRelease;
    2.  
    3.         public void Process(ref InputInteractionContext context)
    4.         {
    5.             if (context.timerHasExpired)
    6.             {
    7.                 context.Canceled();
    8.                 waitUntilRelease = true;
    9.                 return;
    10.             }
    11.  
    12.             if (waitUntilRelease)
    13.             {
    14.                 if (!context.ControlIsActuated(releasePointOrDefault))
    15.                 {
    16.                     waitUntilRelease = false;
    17.                 }
    18.                 else
    19.                 {
    20.                     return;
    21.                 }
    22.             }
    23.  
    24.             if (context.isWaiting && context.ControlIsActuated(pressPointOrDefault))
    25.             {
    26.                 m_TapStartTime = context.time;
    27.                 // Set timeout slightly after duration so that if tap comes in exactly at the expiration
    28.                 // time, it still counts as a valid tap.
    29.                 context.Started();
    30.                 context.SetTimeout(durationOrDefault + 0.00001f);
    31.                 return;
    32.             }
    33.  
    34.             if (context.isStarted && !context.ControlIsActuated(releasePointOrDefault))
    35.             {
    36.                 if (context.time - m_TapStartTime <= durationOrDefault)
    37.                 {
    38.                     context.Performed();
    39.                 }
    40.                 else
    41.                 {
    42.                     ////REVIEW: does it matter to cancel right after expiration of 'duration' or is it enough to cancel on button up like here?
    43.                     context.Canceled();
    44.                 }
    45.             }
    46.         }
    47.         public void Reset()
    48.         {
    49.             waitUntilRelease = false;
    50.             m_TapStartTime = 0;
    51.         }
    52.  
     
    Last edited: Jan 17, 2024