Update touch input settings
This commit is contained in:
@@ -31,6 +31,14 @@ public class InputManager : MonoBehaviour
|
||||
private bool isTouchHeld = false;
|
||||
private bool lastFrameInteracted = false;
|
||||
|
||||
// Tap/drag detection state
|
||||
private Vector2 pressStartPosition;
|
||||
private float pressStartTime;
|
||||
private bool isPressed = false;
|
||||
private bool isDragging = false;
|
||||
private float dragThreshold = 10f; // pixels
|
||||
private float tapTimeThreshold = 0.2f; // seconds
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_instance = this;
|
||||
@@ -76,46 +84,53 @@ public class InputManager : MonoBehaviour
|
||||
private void OnTouchPressStarted(InputAction.CallbackContext ctx)
|
||||
{
|
||||
// Touch started (finger down)
|
||||
Vector3 _screenPos = Camera.main.ScreenToWorldPoint(touchPositionAction.ReadValue<Vector2>());
|
||||
Vector2 screenPos = new Vector2(_screenPos.x, _screenPos.y);
|
||||
lastFrameInteracted = TryDelegateToInteractable(screenPos);
|
||||
Vector2 screenPos = touchPositionAction.ReadValue<Vector2>();
|
||||
Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
|
||||
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
|
||||
lastFrameInteracted = TryDelegateToInteractable(worldPos2D);
|
||||
if (!lastFrameInteracted)
|
||||
defaultConsumer?.OnTouchPress(screenPos);
|
||||
isTouchHeld = true;
|
||||
{
|
||||
pressStartPosition = screenPos;
|
||||
pressStartTime = Time.time;
|
||||
isPressed = true;
|
||||
isDragging = false;
|
||||
defaultConsumer?.OnHoldStart(worldPos2D);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTouchPressCanceled(InputAction.CallbackContext ctx)
|
||||
{
|
||||
// Touch released (finger up)
|
||||
isTouchHeld = false;
|
||||
// Reset lastFrameInteracted for next frame
|
||||
Vector2 screenPos = touchPositionAction.ReadValue<Vector2>();
|
||||
Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
|
||||
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
|
||||
if (!lastFrameInteracted)
|
||||
{
|
||||
float timeHeld = Time.time - pressStartTime;
|
||||
float dist = Vector2.Distance(screenPos, pressStartPosition);
|
||||
if (!isDragging && timeHeld < tapTimeThreshold && dist < dragThreshold)
|
||||
{
|
||||
defaultConsumer?.OnTap(worldPos2D);
|
||||
}
|
||||
defaultConsumer?.OnHoldEnd(worldPos2D);
|
||||
}
|
||||
isPressed = false;
|
||||
isDragging = false;
|
||||
lastFrameInteracted = false;
|
||||
// Optionally, you can notify consumers of release if needed
|
||||
}
|
||||
|
||||
private void OnTouchPositionPerformed(InputAction.CallbackContext ctx)
|
||||
{
|
||||
Vector2 pos = ctx.ReadValue<Vector2>();
|
||||
if (isTouchHeld)
|
||||
{
|
||||
// Convert to world position
|
||||
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
|
||||
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
|
||||
if (!lastFrameInteracted)
|
||||
defaultConsumer?.OnTouchPress(worldPos2D); // Move continuously to finger position
|
||||
}
|
||||
// No longer needed, OnTouchHeld will be handled in Update
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Continuously advertise the last touch position while held
|
||||
if (isTouchHeld && touchPositionAction != null)
|
||||
if (isPressed && touchPositionAction != null)
|
||||
{
|
||||
Vector2 pos = touchPositionAction.ReadValue<Vector2>();
|
||||
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
|
||||
Vector2 screenPos = touchPositionAction.ReadValue<Vector2>();
|
||||
Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
|
||||
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
|
||||
if (!lastFrameInteracted)
|
||||
defaultConsumer?.OnTouchPress(worldPos2D);
|
||||
defaultConsumer?.OnHold(worldPos2D);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +143,7 @@ public class InputManager : MonoBehaviour
|
||||
var interactable = hit.GetComponent<Interactable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.OnTouchPress(worldPos);
|
||||
interactable.OnTap(worldPos);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user