c# - Unity new input system generating multiple events - Stack Overflow
I've just started learning Unity's not-so-new input system. When pressing a button (which causes a UI element to appear and disappear), I'm receiving multiple button press events, which is causing the UI element to flicker (active / inactive) while the user presses the key (for example for a few microseconds). I have been attempting to find a solution that would cause just a single event to be fired regardless of how long the user holds down the key / clicks a button - for example when the key is initially pressed or is released. I have experimented with the Input System Interactions (notably Press: "Release Only" and Tap) but without success - I still get the flickering. I have also been reading about the .WasPerformedThisFrame
method, but I can't figure out how to use it.
I've not experienced the same issue with other buttons such as the on-screen movement and jump controls which work fine - possibly because the jump button, for example, only allows a jump to occur if the player isGrounded
.
I've been using this tutorial to learn the new input system and so I have set up an input controls asset and have the following code in my InputHandler:
public class InputHandler : MonoBehaviour
{
[SerializeField] private InputActionAsset inputControls;
[SerializeField] private string actionMapName = "Unit";
[SerializeField] private string rollup = "Rollup";
private InputAction rollupAction;
public bool RollupTriggered { get; private set; }
public static InputHandler Instance {get; private set; }
private void Awake()
{
...
rollupAction = inputControls.FindActionMap(actionMapName).Find(rollup);
RegisterInputActions();
}
void RegisterInputActions()
{
...
rollupAction.performed += context => RollupTriggered = true;
rollupAction.canceled += context => RollupTriggered = false;
}
private void OnEnable()
{
...
rollupAction.Enable();
}
private void OnDisable()
{
...
rollupAction.Disable();
}
}
Then, in my player / UI controller script, I have the following:
public class UnitController : MonoBehaviour
{
private InputHandler inputHandler;
public GameObject hudPanel;
void Update()
{
...
ShowHideHUD();
}
void ShowHideHUD()
{
if (inputHandler.RollupTriggered)
{
if (hudPanel.activeSelf)
{
hudPanel.SetActive(false);
}
else
{
hudPanel.SetActive(true);
}
}
}
}
I've just started learning Unity's not-so-new input system. When pressing a button (which causes a UI element to appear and disappear), I'm receiving multiple button press events, which is causing the UI element to flicker (active / inactive) while the user presses the key (for example for a few microseconds). I have been attempting to find a solution that would cause just a single event to be fired regardless of how long the user holds down the key / clicks a button - for example when the key is initially pressed or is released. I have experimented with the Input System Interactions (notably Press: "Release Only" and Tap) but without success - I still get the flickering. I have also been reading about the .WasPerformedThisFrame
method, but I can't figure out how to use it.
I've not experienced the same issue with other buttons such as the on-screen movement and jump controls which work fine - possibly because the jump button, for example, only allows a jump to occur if the player isGrounded
.
I've been using this tutorial to learn the new input system and so I have set up an input controls asset and have the following code in my InputHandler:
public class InputHandler : MonoBehaviour
{
[SerializeField] private InputActionAsset inputControls;
[SerializeField] private string actionMapName = "Unit";
[SerializeField] private string rollup = "Rollup";
private InputAction rollupAction;
public bool RollupTriggered { get; private set; }
public static InputHandler Instance {get; private set; }
private void Awake()
{
...
rollupAction = inputControls.FindActionMap(actionMapName).Find(rollup);
RegisterInputActions();
}
void RegisterInputActions()
{
...
rollupAction.performed += context => RollupTriggered = true;
rollupAction.canceled += context => RollupTriggered = false;
}
private void OnEnable()
{
...
rollupAction.Enable();
}
private void OnDisable()
{
...
rollupAction.Disable();
}
}
Then, in my player / UI controller script, I have the following:
public class UnitController : MonoBehaviour
{
private InputHandler inputHandler;
public GameObject hudPanel;
void Update()
{
...
ShowHideHUD();
}
void ShowHideHUD()
{
if (inputHandler.RollupTriggered)
{
if (hudPanel.activeSelf)
{
hudPanel.SetActive(false);
}
else
{
hudPanel.SetActive(true);
}
}
}
}
Share
Improve this question
asked 14 hours ago
MuppetDanceMuppetDance
16411 bronze badges
4
|
1 Answer
Reset to default 0I've implemented a solution, but it's a bit janky and there must be a better way!
The player / UI control script now reads:
public class UnitController : MonoBehaviour
{
private InputHandler inputHandler;
public GameObject hudPanel;
private bool rollupPressed = false;
void Update()
{
...
ShowHideHUD();
}
void ShowHideHUD()
{
if (inputHandler.RollupTriggered)
{
rollupPressed = true;
}
if (rollupPressed && !inputHandler.RollupTriggered)
{
rollupPressed = false;
if (hudPanel.activeSelf)
{
hudPanel.SetActive(false);
}
else
{
hudPanel.SetActive(true);
}
}
}
}
- 百度推出智能云云手机
- 2015年形成的计算机发展趋势 Windows10依然强势
- 移动互联网迅猛发展连接人与服务成为重要趋势
- Windows 10盗版不易?所以在中国普及速度极慢
- 微软要逆天!SurfacePhone能运行exe程序
- reactjs - LLM Endpoint hosted in Azure databricks Response 200 but body is locked - Stack Overflow
- spring boot - This JPQL translation to SQL doesn't make any sense - Stack Overflow
- TypeScript Error (ts2345) when trying to collect property names in array - Stack Overflow
- gdal - Using gdal_translate to compress multiple TIFF files - Stack Overflow
- rust - Basic bracket-lib example crashes with “unsafe precondition(s) violated: slice::from_raw_parts” - Stack Overflow
- x86 - how does internal functions of kernel resolve after paging? - Stack Overflow
- esp32 - Sending and receiving data using ESP32S3 with Ebyte Lora E22 is not working - Stack Overflow
- javascript - Why does this SVG filter not work in a canvas context using Chrome? - Stack Overflow
- javascript - error in getting data from firebase "user": "[object Object]"} in firebase - St
- Scene Appears Completely Black on Android Build Despite Correct UI Rendering in Unity Editor - Stack Overflow
- python - overrideredirect() no longer working on MAC? - Stack Overflow
- postgresql - AzureStorageClient raises the error Unable to determine account name for shared key credential - Stack Overflow
(inputHandler.RollupTriggered)
be true across multiple consequent frames ... – derHugo Commented 1 hour ago