How to Write a Drag and Drop script for Unity 5.3.3?

I am working on a Drag-and-Drop game for 2nd graders, which was assigned to me from school. I have almost no knowledge of coding, but i have set up a field to which i will drag an object into a “drop” object. I have an old script I have been trying to fix for Unity 5.3.3, but it doesn’t work. I need a script I can attach to the “drag” object and the “drop object”.

Here is the Drag script: (I use Visual Studio)

using System;
using System.Collections;
using UnityEngine;

class DragandDrop : MonoBehaviour {

    private Color mouseOverColor = Color.blue;
    private Color originalColor = Color.yellow;
    private bool dragging = false;
    private float distance;


    void OnMouseEnter()
    {
        GetComponet<Renderer> ().material.color = mouseOverColor;
    }

    private object GetComponet<T>()
    {
        throw new NotImplementedException();
    }

    void OnMouseExit()
    {
        GetComponet<Renderer> ().material.color = originalColor;
    }

    void OnMouseDown()
    {
        distance = Vector3.Distance(transform.position, Camera.main.transform.position);
        dragging = true;
    }

    void OnMouseUp()
    {
        dragging = false;
    }

    void Update()
    {
        if (dragging)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Vector3 rayPoint = ray.GetPoint(distance);
            transform.position = rayPoint;
        }
    }
}

I have no current script for the object the item is dragged onto.

You can use the event systems. Below is an example.
Use it on a test scene as follows:

  1. Create a new scene
  2. Add an EventSystem to the scene
  3. Add a Physics Raycaster to the Main Camera
  4. Add a primitive (eg cube) to the scene
  5. Attach the script below to the cube
  6. Fill OnDrag() with whatever functionality you like (eg repositioning the cube based on mouse position which is in the PointerEventData parameter).

All available interfaces are explained in the online documentation under UnityEgine.EventSystems/Interfaces.
It takes a while to figure out but is easy to implement once you understand.

using System;
using UnityEngine;
using UnityEngine.EventSystems;
    
public class DragNDropExample : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    int initialPointerId;

    void Start()
    {
        initialPointerId = int.MaxValue;
    }

	void OnBeginDrag(PointerEventData eventData)
    {
        if (initialPointerId == int.MaxValue)
        {
            initialPointerId = eventData.pointerId;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (initialPointerId == eventData.pointerId)
        {
            initialPointerId = int.MaxValue;
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (initialPointerId == eventData.pointerId)
        {
            // DO WHATEVER YOU WANNA DO DURING DRAGGING HERE
        }
    }
}

@Koen-MattHijs

Thanks for the help, I was confused on the scripting problem until I searched some stuff and found it out. I used your script and this new one i found, which works just as well.

using UnityEngine;
using System.Collections;

public class DragAndDrop : MonoBehaviour
{
    private bool _mouseState;
    private GameObject target;
    public Vector3 screenSpace;
    public Vector3 offset;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // Debug.Log(_mouseState);
        if (Input.GetMouseButtonDown(0))
        {

            RaycastHit hitInfo;
            target = GetClickedObject(out hitInfo);
            if (target != null)
            {
                _mouseState = true;
                screenSpace = Camera.main.WorldToScreenPoint(target.transform.position);
                offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            _mouseState = false;
        }
        if (_mouseState)
        {
            //keep track of the mouse position
            var curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);

            //convert the screen mouse position to world point and adjust with offset
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;

            //update the position of the object in the world
            target.transform.position = curPosition;
        }
    }


    GameObject GetClickedObject(out RaycastHit hit)
    {
        GameObject target = null;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
        {
            target = hit.collider.gameObject;
        }

        return target;
    }
}

Works perfectly now, all I have to do is figure out how to add an element that allows players to write what they want in the “Text boxes” (aka, cubes) and drag them to arrange a word.

@Koen-Matthijs

hi i was wondering if you can help me scripting the drag and drop system.

my aim is to create a CTG of an existing physical version of the game in a digital world. i saw a script that was written by @Rustelk2930. i would like to know if this scripting is best suited for CTG. my aim is to have a hand of card and placing the card on 3D surface in a certain place can you help.