How to drag objects with mouse in unity?

In this article, We will see how to drag objects with mouse in unity. like a sprite or an image UI in unity.

If you are also looking for a way to drag objects with mouse in unity so you are in the right place. For dragging the UI element we will use the IDraggable Interface. For the 3d objects, We will use some custom functions. So let’s begin.

Drag objects with mouse in unity

Before we take a look at the drag scripting part first we should look at the important setup in unity. The only thing that you need to remember is that you need to set your camera to Orthographic Mode for dragging UI elements.

So let’s see the first way to drag objects with the mouse in unity. First, we will look at how to drag UI Images.

Dragging UI Images

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class DraggableUI : MonoBehaviour, IDragHandler
{
    private Vector2 mousePosition = new Vector2();
    private Vector2 startPosition = new Vector2();
    private Vector2 differencePoint = new Vector2();

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0))
        {
            UpdateMousePosition();
        }
        if(Input.GetMouseButtonDown(0))
        {
            UpdateStartPosition();
            UpdateDifferencePoint();
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        /*Minus the difference point so you can pick the 
        element from the edges, without any jerk*/

        transform.position = mousePosition - differencePoint;
    }

    private void UpdateMousePosition()
    {
        mousePosition.x = Input.mousePosition.x;
        mousePosition.y = Input.mousePosition.y;
    }

    private void UpdateStartPosition()
    {
        startPosition.x = transform.position.x;
        startPosition.y = transform.position.y;
    }

    private void UpdateDifferencePoint()
    {
        differencePoint = mousePosition - startPosition;
    }
}

In the above code, we have used the IDragHandler Interface.

So in the callback, we get OnDrag Functions and it gets a call when dragging is occurring this will be called every time the cursor is moved.

So this is how we drag objects with the mouse in unity. Now you can simply attach this script to your UI Image to make that draggable.

How to drag objects with mouse in unity?
drag objects with mouse in unity

Dragging 2D Sprites or 3D object​

For dragging 2D Sprites or 3D game objects we use a different approach. So here is the simple code

using UnityEngine;

public class Draggable : MonoBehaviour
{
    public Camera myCam;
    
    private float startXPos;
    private float startYPos;

    private bool isDragging = false;

    private void Update()
    {
        if (isDragging)
        {
            DragObject();
        }
    }

    private void OnMouseDown()
    {
        Vector3 mousePos = Input.mousePosition;

        if (!myCam.orthographic)
        {
            mousePos.z = 10;
        }

        mousePos = myCam.ScreenToWorldPoint(mousePos);

        startXPos = mousePos.x - transform.localPosition.x;
        startYPos = mousePos.y - transform.localPosition.y;

        isDragging = true;
    }

    private void OnMouseUp()
    {
        isDragging = false;
    }

    public void DragObject()
    {
        Vector3 mousePos = Input.mousePosition;

        if(!myCam.orthographic)
        {
            mousePos.z = 10;
        }

        mousePos = myCam.ScreenToWorldPoint(mousePos);
        transform.localPosition = new Vector3(mousePos.x - startXPos, mousePos.y - startYPos, transform.localPosition.z);
    }
}

Simply drag this script to your sprite renderer object or any 3D game object to make that draggable. So this is how you drag objects with mouse in unity.

Hope it was helpful and you learned something new. Let me know if you have any doubts or queries or any kind of issues you are facing in unity.

So what is your favorite way of dragging objects?

You can also read our other topics likeCoroutines, Camera Shake, Camera Follow, Camera Clamping, Ways of finding gameobjects at runtime, etc.

Check out our YouTube Video on Dragging UI elements in Unity.

2 thoughts on “How to drag objects with mouse in unity?”

  1. Hello, I would like to know why when I move mouse on x axis it moves the object so fast and crazy, on Y axis it is fine but it’s doing on x axis and I dont know why

Leave a Comment

Your email address will not be published.