[Puzzles][Input] Update input handling to not mistakingly miss frames. Add debug frame input debugging.

This commit is contained in:
Michal Pikulski
2025-09-03 16:55:21 +02:00
parent 93242b2702
commit 1dfcee3d2c
9 changed files with 196 additions and 26 deletions

View File

@@ -4,22 +4,41 @@ using System;
public class InputManager : MonoBehaviour
{
public static InputManager Instance { get; private set; }
private static InputManager _instance;
public static InputManager Instance
{
get
{
if (_instance == null)
{
_instance = FindAnyObjectByType<InputManager>();
if (_instance == null)
{
var go = new GameObject("InputManager");
_instance = go.AddComponent<InputManager>();
DontDestroyOnLoad(go);
}
}
return _instance;
}
}
private PlayerInput playerInput;
private InputAction touchPressAction;
private InputAction touchPositionAction;
private ITouchInputConsumer defaultConsumer;
private bool isTouchHeld = false;
private bool lastFrameInteracted = false;
void Awake()
{
if (Instance != null && Instance != this)
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
_instance = this;
DontDestroyOnLoad(gameObject);
playerInput = GetComponent<PlayerInput>();
if (playerInput == null)
@@ -62,17 +81,20 @@ public class InputManager : MonoBehaviour
private void OnTouchPressStarted(InputAction.CallbackContext ctx)
{
// Touch started (finger down)
isTouchHeld = true;
Vector3 _screenPos = Camera.main.ScreenToWorldPoint(touchPositionAction.ReadValue<Vector2>());
Vector2 screenPos = new Vector2(_screenPos.x, _screenPos.y);
if (!TryDelegateToInteractable(screenPos))
lastFrameInteracted = TryDelegateToInteractable(screenPos);
if (!lastFrameInteracted)
defaultConsumer?.OnTouchPress(screenPos);
isTouchHeld = true;
}
private void OnTouchPressCanceled(InputAction.CallbackContext ctx)
{
// Touch released (finger up)
isTouchHeld = false;
// Reset lastFrameInteracted for next frame
lastFrameInteracted = false;
// Optionally, you can notify consumers of release if needed
}
@@ -84,11 +106,8 @@ public class InputManager : MonoBehaviour
// Convert to world position
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
defaultConsumer?.OnTouchPress(worldPos2D); // Move continuously to finger position
}
else
{
defaultConsumer?.OnTouchPosition(pos); // For drag or hover
if (!lastFrameInteracted)
defaultConsumer?.OnTouchPress(worldPos2D); // Move continuously to finger position
}
}
@@ -100,7 +119,8 @@ public class InputManager : MonoBehaviour
Vector2 pos = touchPositionAction.ReadValue<Vector2>();
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
defaultConsumer?.OnTouchPress(worldPos2D);
if (!lastFrameInteracted)
defaultConsumer?.OnTouchPress(worldPos2D);
}
}