Unity 3D Tutorial – How to Drag and Drop Objects Using Touch Controls with C#

To continue with our exploration of Unity Mobile capabilities this time we will explore how to make a simple project where you can drag and drop multiple objects inside of a game scene using touch and drag controls. This is a important part of mobile games and app development because on mobile and tablets you will only have the touch controls to play and navigate inside of a app or game.

Lesson objectives –

  1. Drag and drop game objects inside of unity using C# programming
  2. Use the native C# methods inside of unity to make a drag and drop app
  3. Change colours of game object through he C# script
  4. Create selection for Touch Inputs including TouchPhase.Began, TouchPhase.Moved and TouchPhase.Ended


Video Tutorial on How to make a Drag and Drop with Touch Controls in Unity


Colour Change Script –

This script will be attached to the cubes. This script has a simple Color class instance and then we will use that public variable. We will have access to the colour pallet from the unity inspector and then it will save that colour inside of the activeColor variable and then it will be applied to the box when the scene runs.


Dragging and Dropping Touch Script –

This script will be attached to the Camera inside of the scene. This script will run through the update function which runs on every frame to check when the touch began, moved and ended. With each of these conditions we will ask the script to complete a set of instructions. When the touch phase begins we will link the game object that gets reference to the cube we tap on and then it will save a local copy of it. From that copy we will be able to change position of it using multiple Vector3 variables.


Comment on this tutorial and let us know how you got on -