Welcome to this unity 2d drag and drop tutorial. Where we are going to create a basic script so that you can have drag and drop functionality in your unity games. For if you want to do inventory management, hyper casual drag and drop mechanics and more. To check out the video tutorial check out the video below:

You will learn how to do a ondrag, how to drag 2d objects. With this information you will quickly be able to implement unity drag and drop for any of your game objects.

Creating a drag and drop unity blank project

First we will start off by creating a blank unity 2d project. We will be adding a basic square and a script to achieve this drag and drop mechanic. So i start off with a blank unity project like so.

unity drag and drop tutorial – blank project

Next add a few folder like below, we will be adding a object folder and a scripts folder.

unity drag and drop tutorial – folders

In the objects folder right click and create a sprite->square.

unity drag and drop tutorial – create square sprite

Once you have your square you need to add a boxcollider2d to it.

unity drag and drop tutorial – box collider 2d

Next in the scripts folder right click and create a c# script called DragAndDrop.

unity drag and drop tutorial – c# script

Once done add your script to your square and we are almost ready to write our code.

unity drag and drop tutorial -attach script

Writing our drag and drop code for our unity project

So just to explain what we will be doing. We will be taking a mouse input and allowing our object to be dragged around. We will do this by checking our colliders from our mouse position and our square and if we are clicking on our square we will be able to drag it around using states we have set inside of our code.

Now open up your c# script. The first thing we want to do is declare some variables. One of which is a Collider2D, canMove which is a boolean and a variable called dragging which is a boolean. We will initially set our dragging and canMove variables to false and set our Collider2D to our square’s collider. To do this look at the below code.





using System.Collections; using System.Collections.Generic; using UnityEngine; public class DragAndDrop : MonoBehaviour { bool canMove; bool dragging; Collider2D collider; void Start() { collider = GetComponent<Collider2D>(); canMove = false; dragging = false; }

After this we need to start by getting our mouse position relative to our world. For that we need to use our main camera and set a mousePos variable to a Screentoworldpoint conversion of our mouse position. Once we have done this we need to detect a mouse button down click and do a check to see if our mouse position is overlapping our square’s collider.

If we are clicking on our object allow our square to move and set a dragging flag. Then we finally need to check that our mouse button has been lifted and set all our flags to false.

Here is the code you need to make this work.

// Update is called once per frame void Update() { Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (Input.GetMouseButtonDown(0)) { if (collider == Physics2D.OverlapPoint(mousePos)) { canMove = true; } else { canMove = false; } if (canMove) { dragging = true; } } if (dragging) { this.transform.position = mousePos; } if (Input.GetMouseButtonUp(0)) { canMove = false; dragging = false; } } }

Running our code

Head back over to unity and hit the play button you should now be able to drag the square around in your scene. It should look something like this below.

Great so we have done this now with mouse input let’s see how we can do this with the mobile input.

Unity mobile drag and drop

Let us now use the code we have and look at how we can use the mobile input instead to perform a drag and drop. Natively unity handles touch with the getmousebuttondown, however we want to be sure that this will work on all devices so we will use touch count as well. We will use touch counts to help us and other features of the mobile input system. Here is the full code. I will explain below.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class DragAndDrop : MonoBehaviour { bool canMove; bool dragging; Collider2D collider; void Start() { collider = GetComponent<Collider2D>(); canMove = false; dragging = false; } // Update is called once per frame void Update() { Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (Input.GetMouseButtonDown(0) || Input.touchCount == 1) { if (collider == Physics2D.OverlapPoint(mousePos)) { canMove = true; } else { canMove = false; } if (canMove) { dragging = true; } } if (dragging) { this.transform.position = mousePos; } if (Input.GetMouseButtonUp(0) || Input.touchCount <= 0) { canMove = false; dragging = false; } } }

So the only new thing added to make the drag and drop work better with mobile is the detection of touch count. This just helps us make sure the touch is actually happening on the mobile device. So you will see in the getmousebuttonup section we now also check for touchCount of less than equal to zero, so basically if no touches or in the off chance of a anomaly of negative touch counts we assume this to me a mouseup. In the get mouse down section we have Input.touchCount == 1 which makes sure we only detect one touch and not multiple to avoid any weird bugs where the mobile device will not be able to determine where exactly we are touching.

Some frequently asked questions about unity drag and drop functionality

How do you drag objects in Unity 2d? If you want to add drag and drop in unity you will need to create a c# script which handles the states of touch/click,dragging and moving and then of releasing a button or touch on a mobile device.

How do you drag and drop in unity? In order to add drag and drop in unity you will need to add a c# script to handle the events of touch/click,dragging and moving and then finally releasing a button or touch on a mobile device.

Does unity have drag and drop? Unity does not natively support this. Which means you need to implement some c# code in order to make drag and drop work within your unity project.

How do you drag on Android? To allow for mobile drag and drop you need to use the mobile input system in unity. Which will require you to keep track of touch points.

How do you move objects with touch in unity? If you want to move objects in unity using touch you will need to use touchpoints functionality in your c# scripts.

If you liked this tutorial be sure to check out some of my other tutorials:

Unity 2D platformer controller wall jump tutorial

(Affiliate link, support me in order for me to keep up the content creation) Check out my paid skillshare course here: https://skl.sh/2YhzEfe