Search Unity

Drag and Drop Scripts?

Discussion in 'General Discussion' started by MikezNesh, Nov 17, 2010.

  1. MikezNesh

    MikezNesh

    Joined:
    Jun 18, 2010
    Posts:
    37
    Has anyone ever created a drag and drop script for unity? Or maybe even a demo? If so, can you show me a link? Thanks.

    EDIT: Or if not, how to make one.

    Here is what I think the process would be like:

    1) Set up a script to tell it when your dragging and dropping
    2) Set the object hologram to follow your mouse position
    3) Instantiate at release point
    4) Add delete button and move options


    I can do #3 and most of #4 but not sure about 1 2....
     
    Last edited: Nov 17, 2010
  2. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    Yeah unity has a drag and drop script its one of the standard assets.
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The Drag Rigidbody script (in Unity's standard assets as aiursrage2k said) will probably help if you are dragging objects in the scene but if you want to do this in GUI then check out this thread for an example.
     
  4. bobin115

    bobin115

    Joined:
    Jun 4, 2013
    Posts:
    13
    i have used this drag and drop script in my game just add a ridged body to it and a collider

    Code (csharp):
    1. //Script to drag an object in world space using the mouse
    2.  
    3.  
    4.  
    5.  
    6.  
    7. var screenSpace;
    8.  
    9. var offset;
    10.  
    11.  
    12.  
    13. function OnMouseDown(){
    14.  
    15.     //translate the cubes position from the world to Screen Point
    16.  
    17.     screenSpace = Camera.main.WorldToScreenPoint(transform.position);
    18.  
    19.      
    20.  
    21.     //calculate any difference between the cubes world position and the mouses Screen position converted to a world point  
    22.  
    23.     offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
    24.  
    25.    
    26.  
    27. }
    28.  
    29.  
    30.  
    31. /*
    32.  
    33. OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
    34.  
    35. OnMouseDrag is called every frame while the mouse is down.
    36.  
    37. */
    38.  
    39.  
    40.  
    41. function OnMouseDrag () {
    42.  
    43.  
    44.  
    45.     //keep track of the mouse position
    46.  
    47.     var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);    
    48.  
    49.  
    50.  
    51.     //convert the screen mouse position to world point and adjust with offset
    52.  
    53.     var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
    54.  
    55.  
    56.  
    57.     //update the position of the object in the world
    58.  
    59.     transform.position = curPosition;
    60.  
    61. }
     
  5. sicga123

    sicga123

    Joined:
    Jan 26, 2011
    Posts:
    782
    There is another drag drop script on the unity wiki. A bit more precise than the regular one.
     
  6. Sayan

    Sayan

    Joined:
    May 11, 2013
    Posts:
    5
    Halo Sicga .
    Will you please say where to find unity wiki ?
    Thanks.
     
  7. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324