Skip to content

Instantly share code, notes, and snippets.

@stevensrmiller
Created April 1, 2022 13:11
Show Gist options
  • Save stevensrmiller/6aaa9edec388d71f696205b7158836c2 to your computer and use it in GitHub Desktop.
Save stevensrmiller/6aaa9edec388d71f696205b7158836c2 to your computer and use it in GitHub Desktop.
Use the Unity Input System to drag an object by dynamically adding and removing event handlers.
// This example calls for an object to be designated for dragging
// by explicitly setting a Transform. That's enough to demonstrate
// the method. Real production code would need to use a Raycast to
// find the object to drag. It would be much more sensible in that
// setting to have the MoveStarted, MovePerformed, and MoveCanceled
// methods supplied to this code by the object being dragged. That
// would allow any kind of "dragging," such as turning a dial, or
// moving within limts. This video goes into that in detail:
//
// https://youtu.be/WjPDPyt3pBY
using UnityEngine;
using UnityEngine.InputSystem;
public class Dragger : MonoBehaviour
{
public Transform dragObject;
public float scale = 20;
public InputAction moveAction;
public InputAction dragAction;
Mouse mouse;
Vector2 startOffset;
void Start()
{
mouse = Mouse.current;
moveAction.Enable();
dragAction.Enable();
dragAction.started += DragStarted;
dragAction.canceled += DragCanceled;
}
void DragStarted(InputAction.CallbackContext c)
{
Debug.Log("Drag Started");
moveAction.started += MoveStarted;
moveAction.performed += MovePerformed;
moveAction.canceled += MoveCanceled;
}
void DragCanceled(InputAction.CallbackContext c)
{
Debug.Log("Drag Canceled");
moveAction.started -= MoveStarted;
moveAction.performed -= MovePerformed;
moveAction.canceled -= MoveCanceled;
}
void MoveStarted(InputAction.CallbackContext c)
{
Debug.Log($"Started {mouse.delta.ReadValue()} {mouse.position.ReadValue()}");
startOffset = mouse.delta.ReadValue();
}
void MovePerformed(InputAction.CallbackContext c)
{
Debug.Log($"Performed {mouse.delta.ReadValue()} {mouse.position.ReadValue()}");
Vector2 totalOffset = mouse.delta.ReadValue() - startOffset;
Vector3 position = dragObject.position;
position.x = position.x + totalOffset.x / scale;
position.y = position.y + totalOffset.y / scale;
dragObject.position = position;
}
void MoveCanceled(InputAction.CallbackContext c)
{
Debug.Log($"Canceled {mouse.delta.ReadValue()} {mouse.position.ReadValue()}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment