Hi, I’m trying to implement Drag & Drop with new Input System. The problem is that when I tap, the mouse coordinates in the log are always (0,0). I tried changing the ActionType to Button and PassThrough, but it didn’t help. How can I get the correct coordinates?
Input System 1.9.0, Unity 6000.0.16f1.
public class InputService : IInputService, IInitializable, IDisposable
{
private readonly ILogService _logService;
private InputActions _inputActions;
private Vector2 _clickPosition;
private bool _isDragging;
[Inject]
public InputService(ILogService logService)
{
_logService = logService;
}
public void Initialize()
{
_inputActions = new InputActions();
_inputActions.Enable();
Subscribe();
}
public void Dispose()
{
Unsubscribe();
_inputActions.Disable();
}
private void Subscribe()
{
_inputActions.Gameplay.Tap.performed += TapPerformed;
_inputActions.Gameplay.Hold.performed += HoldPerformed;
_inputActions.Gameplay.Hold.canceled += HoldCanceled;
_inputActions.Gameplay.PointerPosition.performed += PointerPositionPerformed;
}
private void Unsubscribe()
{
_inputActions.Gameplay.Tap.performed -= TapPerformed;
_inputActions.Gameplay.Hold.performed -= HoldPerformed;
_inputActions.Gameplay.Hold.canceled -= HoldCanceled;
_inputActions.Gameplay.PointerPosition.performed -= PointerPositionPerformed;
}
private void TapPerformed(InputAction.CallbackContext context)
{
Vector2 pos = context.ReadValue<Vector2>();
_logService.Log($"tap {pos}");
}
private void HoldPerformed(InputAction.CallbackContext context)
{
Vector2 pos = context.ReadValue<Vector2>();
_logService.Log($"hold {pos}");
_isDragging = true;
}
private void HoldCanceled(InputAction.CallbackContext context)
{
_isDragging = false;
}
private void PointerPositionPerformed(InputAction.CallbackContext context)
{
if (!_isDragging)
return;
Vector2 pos = context.ReadValue<Vector2>();
_logService.Log($"drag {pos}");
}
}