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.

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

public class ChangingColor : MonoBehaviour
{
    public Color activeColor;

    // Start is called before the first frame update
    void Start()
    {
        GetComponent<MeshRenderer>().material.color = activeColor;
    }
}

 

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.

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

public class Dragging : MonoBehaviour
{
    private float dist;
    private bool dragging = false;
    private Vector3 offset;
    private Transform toDrag;

    // Update is called once per frame
    void Update()
    {
        
        Vector3 v3;

        if (Input.touchCount != 1)
        {
            dragging = false;
            return;
        }

        Touch touch = Input.touches[0];
        Vector3 pos = touch.position;

        if (touch.phase == TouchPhase.Began)
        {
            Ray ray = Camera.main.ScreenPointToRay(pos);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.tag == "cube")
                {
                    toDrag = hit.transform;
                    dist = hit.transform.position.z - Camera.main.transform.position.z;
                    v3 = new Vector3(pos.x, pos.y, dist);
                    v3 = Camera.main.ScreenToWorldPoint(v3);
                    offset = toDrag.position - v3;
                    dragging = true;
                }
            }
        }

        if (dragging && touch.phase == TouchPhase.Moved)
        {
            v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
            v3 = Camera.main.ScreenToWorldPoint(v3);
            toDrag.position = v3 + offset;
        }

        if (dragging && (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
        {
            dragging = false;
        }
    }
}

 




Comments are closed.