Implement a Drag and Drop Script with C#

I´ve tried for some hours to create a script that simply translates the clicked object with the mouse button along the X and Y axis. After several searches a tries, I just can´t understand how to do it.

The best I can do was follow this code:

public Vector3 point;

void OnMouseDrag() {
    point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    transform.position = point;
}

and this:

void OnMouseDrag() {
    Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    point.y = gameObject.transform.position.y;
    gameObject.transform.position = point;
}

Also I tried to use raycast, but can´t realize how to return the hit object. The real problem is that I can´t make the object follow the mouse. Sometimes It just “jumps” for other location.

Can someone give me a direction or a explanation?

Thanks!!!

3 Likes

I’ve used the following class to drag transforms. Should be put on the object to drag, of course.

using System.Collections;
using UnityEngine;

class DragTransform : MonoBehaviour
{
    private Color mouseOverColor = Color.blue;
    private Color originalColor = Color.yellow;
    private bool dragging = false;
    private float distance;

    
    void OnMouseEnter()
    {
        renderer.material.color = mouseOverColor;
    }

    void OnMouseExit()
    {
        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;
        }
    }
}
14 Likes

Well, the basic idea for a drag and drop script would be as follows:

  1. Check to see if the mouse is over the draggable object by using a Raycast.
  2. If the mouse is over the object, check to see if the mouse is clicked
  3. Then as long as the mouse is clicked, move the object to the mouse position. Make sure that for this part that the mouse does not need to be over the object. The mouse needs to be over the object when the mouse is first clicked, but for as long as the mouse is held it does not need to be over the object. To move the object on a plane just change the x and y coordinates of the object’s position, rather than all three coordinates.
1 Like

Tobias J., it’s working perfectly!!!

Atin Skrita, with these steps now I can understand clearly the code and how apply it.

Thank you both!!!

1 Like

i get errors at:

void OnMouseDown()
{
distance = Vector3.Distance(transform.position, Camera.main.transfrom.position);
dragging = true;
}
it says: error CS1061: Type UnityEngine.Camera' does not contain a definition for transform’ and no extension
method transform' of type UnityEngine.Camera’ could be found (are you missing a using directive or an assembly reference?)

1 Like

Tobias J. i used your script but when i click it it just disapears… How do i fix this?

Since this has been bumped here is a video tutorial for doing drag and drop with the new event system and UI components.

8 Likes

i need help
@Kiwasi [/URL]can u put the code here?

There is a link in the video description to my bitbucket repo, which has all the code.

using System.Collections;
using UnityEngine;

class DragTransform : MonoBehaviour
{
    private Color mouseOverColor = Color.blue;
    private Color originalColor = Color.yellow;
    private bool dragging = false;
    private float distance;
    private Vector3 startDist;

   
    void OnMouseEnter()
    {
        renderer.material.color = mouseOverColor;
    }

    void OnMouseExit()
    {
        renderer.material.color = originalColor;
    }

    void OnMouseDown()
    {
        distance = Vector3.Distance(transform.position, Camera.main.transform.position);
        dragging = true;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Vector3 rayPoint = ray.GetPoint(distance);
        starDist = transform.position - rayPoint;
    }

    void OnMouseUp()
    {
        dragging = false;
    }

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

I modified your code a little, so it is not affected by where you click on the object.

2 Likes

Here is a good text based version for this tutorial. Unity drag and drop tutorial blog post or you can watch the video on youtube here.

1 Like

Here is a simple drag and drop tutorial.

1 Like

Here is a drag and drop from UI space to world space implementation which maintain object rotation.

There is a link to asset store in the bottom of the video