Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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