Hello,
I want to share my solution for an easy Mouse Drag and Move.
Before all: The Drag and Move solution itself (with the old input system) comes from this youtube tutorial:
Thank you Game Dev Guide for the solution!
I wanted to use the same solution with the new input system. I’ve watched countless numbers of tutorials, but most of them was really complex, and in my project I want to change Control Schemes and Action Maps as well, therefore I could not allow hard coding of devices.
In This case I’m moving a camera which is on a Camera Rig.
The script itself has been attached to the camera Rig as a component.
Here you can see the Actions configuration:
As You can see I’m using a Mouse Delta to keep the action triggering and separate the two different input types within code. I’ve not tried, but maybe mouse position could work as the same with the option of getting the mouse position as additional information.
Here is the backbone of the Method which is being triggered by DragAndMove Action (via Player Input Component and Invoking Unity Events):
public void ClickHoldRelease(InputAction.CallbackContext context)
{
System.Type vector2Type = Vector2.zero.GetType();
string buttonControlPath = "/Mouse/leftButton";
//string deltaControlPath = "/Mouse/delta";
Debug.Log(context.control.path);
//Debug.Log(context.valueType);
if (context.started)
{
if (context.control.path == buttonControlPath)
//if (context.valueType != vector2Type)
{
Debug.Log("Button Pressed Down Event - called once when button pressed");
}
}
else if (context.performed)
{
if (context.control.path == buttonControlPath)
//if (context.valueType != vector2Type)
{
Debug.Log("Button Hold Down - called continously till the button is pressed");
}
}
else if (context.canceled)
{
if (context.control.path == buttonControlPath)
//if (context.valueType != vector2Type)
{
Debug.Log("Button released");
}
}
}
As you can see, you can check Callbackcontext.valueType as well as Control path.
and here is the Script with the Drag and Move :
using UnityEngine;
using UnityEngine.InputSystem;
public class CamController : MonoBehaviour
{
Camera cam;
Vector3 newPosition;
[SerializeField] private float movementTime = 5f;
Vector3 dragStartPosition = Vector3.zero;
Vector3 dragCurrentPosition = Vector3.zero;
private void Start()
{
newPosition = transform.position;
cam = Camera.main;
}
private void Update()
{
ApplyMovements();
}
public void DragAndMove(InputAction.CallbackContext context)
{
System.Type vector2Type = Vector2.zero.GetType();
string buttonControlPath = "/Mouse/leftButton";
//string deltaControlPath = "/Mouse/delta";
if (context.started)
{
if (context.control.path == buttonControlPath)
{
Debug.Log("Button Pressed Down Event - called once when button pressed");
Ray dragStartRay = cam.ScreenPointToRay(Mouse.current.position.ReadValue());
Plane dragStartPlane = new Plane(Vector3.up, Vector3.zero);
float dragStartEntry;
if (dragStartPlane.Raycast(dragStartRay, out dragStartEntry))
{
dragStartPosition = dragStartRay.GetPoint(dragStartEntry);
}
}
}
else if (context.performed)
{
if (context.control.path == buttonControlPath)
{
Debug.Log("Button Hold Down - called continously till the button is pressed");
Ray dragCurrentRay = cam.ScreenPointToRay(Mouse.current.position.ReadValue());
Plane dragCurrentPlane = new Plane(Vector3.up, Vector3.zero);
float dragCurrentEntry;
if (dragCurrentPlane.Raycast(dragCurrentRay, out dragCurrentEntry))
{
dragCurrentPosition = dragCurrentRay.GetPoint(dragCurrentEntry);
newPosition = transform.position + dragStartPosition - dragCurrentPosition;
}
}
}
else if (context.canceled)
{
if (context.control.path == buttonControlPath)
{
Debug.Log("Button released");
}
}
}
private void ApplyMovements()
{
transform.position = Vector3.Lerp(transform.position, newPosition, movementTime * Time.deltaTime);
}
}
I know Mouse.current.position.ReadValue() is hard coding the Mouse Device, but keep in mind without mouse Device this could be not even called, because the binding requires a Mouse device itself.
I don’t think it’s the best solution, but it’s really simple and I wanted to share it.
Thanks for reading! Enjoy Coding!






