Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Implement a Drag and Drop Script with C#

Discussion in 'Scripting' started by Leo-Ferreira, Apr 2, 2012.

  1. Leo-Ferreira

    Leo-Ferreira

    Joined:
    Jan 9, 2012
    Posts:
    14
    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:
    Code (csharp):
    1.  
    2. public Vector3 point;
    3.  
    4. void OnMouseDrag() {
    5.     point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    6.     transform.position = point;
    7. }
    8.  
    and this:

    Code (csharp):
    1.  
    2. void OnMouseDrag() {
    3.     Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.     point.y = gameObject.transform.position.y;
    5.     gameObject.transform.position = point;
    6. }
    7.  
    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!!!
     
  2. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    I've used the following class to drag transforms. Should be put on the object to drag, of course.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. class DragTransform : MonoBehaviour
    6. {
    7.     private Color mouseOverColor = Color.blue;
    8.     private Color originalColor = Color.yellow;
    9.     private bool dragging = false;
    10.     private float distance;
    11.  
    12.    
    13.     void OnMouseEnter()
    14.     {
    15.         renderer.material.color = mouseOverColor;
    16.     }
    17.  
    18.     void OnMouseExit()
    19.     {
    20.         renderer.material.color = originalColor;
    21.     }
    22.  
    23.     void OnMouseDown()
    24.     {
    25.         distance = Vector3.Distance(transform.position, Camera.main.transform.position);
    26.         dragging = true;
    27.     }
    28.  
    29.     void OnMouseUp()
    30.     {
    31.         dragging = false;
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         if (dragging)
    37.         {
    38.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    39.             Vector3 rayPoint = ray.GetPoint(distance);
    40.             transform.position = rayPoint;
    41.         }
    42.     }
    43. }
    44.  
     
  3. Atin Skrita

    Atin Skrita

    Joined:
    Feb 18, 2011
    Posts:
    94
    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.
     
    Lethn likes this.
  4. Leo-Ferreira

    Leo-Ferreira

    Joined:
    Jan 9, 2012
    Posts:
    14
    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!!!
     
    Nidhi7045 likes this.
  5. Quigorath

    Quigorath

    Joined:
    Oct 12, 2012
    Posts:
    1
    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?)
     
    Nidhi7045 likes this.
  6. DonutProgramm

    DonutProgramm

    Joined:
    Mar 23, 2015
    Posts:
    1
    Tobias J. i used your script but when i click it it just disapears... How do i fix this?
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Since this has been bumped here is a video tutorial for doing drag and drop with the new event system and UI components.

     
  8. fezaterka

    fezaterka

    Joined:
    Jun 11, 2019
    Posts:
    2
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is a link in the video description to my bitbucket repo, which has all the code.
     
  10. TheDarkVoice

    TheDarkVoice

    Joined:
    Jan 31, 2016
    Posts:
    3
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. class DragTransform : MonoBehaviour
    6. {
    7.     private Color mouseOverColor = Color.blue;
    8.     private Color originalColor = Color.yellow;
    9.     private bool dragging = false;
    10.     private float distance;
    11.     private Vector3 startDist;
    12.  
    13.    
    14.     void OnMouseEnter()
    15.     {
    16.         renderer.material.color = mouseOverColor;
    17.     }
    18.  
    19.     void OnMouseExit()
    20.     {
    21.         renderer.material.color = originalColor;
    22.     }
    23.  
    24.     void OnMouseDown()
    25.     {
    26.         distance = Vector3.Distance(transform.position, Camera.main.transform.position);
    27.         dragging = true;
    28.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    29.         Vector3 rayPoint = ray.GetPoint(distance);
    30.         starDist = transform.position - rayPoint;
    31.     }
    32.  
    33.     void OnMouseUp()
    34.     {
    35.         dragging = false;
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         if (dragging)
    41.         {
    42.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    43.             Vector3 rayPoint = ray.GetPoint(distance);
    44.             transform.position = rayPoint + startDist;
    45.         }
    46.     }
    47. }
    48.  

    I modified your code a little, so it is not affected by where you click on the object.
     
    davidlopez95435 and Procy2 like this.
  11. gpaulguilfoyle

    gpaulguilfoyle

    Joined:
    Oct 29, 2019
    Posts:
    3
    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.
     
    Bear_Operator likes this.
  12. prasanthvel

    prasanthvel

    Joined:
    Aug 21, 2017
    Posts:
    7
    Here is a simple drag and drop tutorial.
     
    Misbahkousar likes this.
  13. the_coffee_std

    the_coffee_std

    Joined:
    Jul 8, 2020
    Posts:
    1
    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