Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Feature Request Continuous Interaction

Discussion in 'Input System' started by Casey1289719827496895, May 5, 2022.

  1. Casey1289719827496895

    Casey1289719827496895

    Joined:
    May 4, 2022
    Posts:
    1
    I'm gonna keep it simple: can I pr this script somewhere?

    Code (CSharp):
    1. #nullable enable
    2. using UnityEngine;
    3. using UnityEngine.InputSystem;
    4.  
    5. namespace UnityExtras.Input
    6. {
    7. #if UNITY_EDITOR
    8.     [UnityEditor.InitializeOnLoad]
    9. #endif
    10.     public class ContinuousInteraction : IInputInteraction
    11.     {
    12.         public float activationTime { get; private set; }
    13.         private float lastDeltaTime;
    14.  
    15.         static ContinuousInteraction()
    16.         {
    17.             InputSystem.RegisterInteraction<ContinuousInteraction>();
    18.         }
    19.  
    20.         public void Process(ref InputInteractionContext context)
    21.         {
    22.             if (!context.ControlIsActuated())
    23.             {
    24.                 lastDeltaTime = activationTime = 0f;
    25.                 context.Canceled();
    26.             }
    27.             else if (context.phase.IsInProgress())
    28.             {
    29.                 activationTime += lastDeltaTime;
    30.                 if (!context.action.WasPerformedThisFrame())
    31.                 {
    32.                     context.PerformedAndStayPerformed();
    33.                 }
    34.                 context.SetTimeout(0.000001f);
    35.                 lastDeltaTime = Time.deltaTime;
    36.             }
    37.             else
    38.             {
    39.                 context.Started();
    40.                 Process(ref context);
    41.             }
    42.         }
    43.  
    44.         public void Reset()
    45.         {
    46.         }
    47.     }
    48. }
    49.  
    I'll be honest this hasn't been tested rigorously against cases like multiple interactions, different controllers, processors, but I think its simplicity speaks to its strength: continuously perform your action so long as the control is actuated.
     
    Last edited: May 6, 2022