Search Unity

Drag drop not quite working right

Discussion in 'Scripting' started by giza, Jun 18, 2008.

  1. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    I'm dragging an object from one position to another. The target object has "Is Trigger" checked. The object that I'm dragging snaps back to its source position onMouseUp if outside the trigger area of the target object but it doesn't snap into position when it hits the trigger of the target object - it just sort of stops where it overlaps the target, not on the coordinates in my code.

    Code (csharp):
    1. var curPos =0;
    2. var curScreenPos : Vector3;
    3. var trigger = false;
    4.  
    5. function OnMouseDown () {
    6.    var screenPos = Camera.main.WorldToScreenPoint(transform.position);
    7.    var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z));
    8.    while (Input.GetMouseButton(0))
    9.    {
    10.       var curScreenPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z);
    11.       var curPos = Camera.main.ScreenToWorldPoint(curScreenPos) + offset;
    12.       transform.position = curPos;
    13.       yield;
    14.       print (curPos);  
    15.    }
    16. }
    17.  
    18. function OnTriggerEnter (other : Collider) {
    19.    transform.position = Vector3(3.0,0,0);
    20.    trigger = true; 
    21. }
    22.  
    23. function OnTriggerExit (other : Collider){
    24.     transform.position = Vector3(-0.7,0,0);
    25.     trigger = false;    
    26. }
    27.  
    28. function OnMouseUp () {
    29.    
    30.     if (trigger == false){
    31.     transform.position = Vector3(-0.7,0,0);  
    32.    }
    33. }
    34.  
    Can anybody please give me an idea of where I'm going wrong and why the object doesn't automatically snap to Vector3(3.0,0,0) when I release the mouse over the target object? Thanks!
     
  2. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    Ok, sorry. Stupid of me! Forgot to add the else statement at the end.

    Code (csharp):
    1. function OnMouseUp () {
    2.    
    3.    if (trigger == false){
    4.    transform.position = Vector3(-0.7,0,0);    
    5.    }
    6.  
    7.    else{
    8.    transform.position = Vector3(3.0,0,0);
    9.    }
    10. }
    11.  
    Learning as I go, learning as I go .....
     
  3. rmlloyd

    rmlloyd

    Joined:
    Jul 22, 2008
    Posts:
    82
    Hi,
    I am new to unity, the code above looks excellent. Where does code like this go in unity? do you have to attach it to a particular object for it to run?

    Thanks alot
    Ryan
     
  4. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    Yes. You have to attach the code to the object that you are dragging from point A to point B. In my case I've attached the code and a Rigidbody to the cube that I'm dragging between two positions that are "marked" out by two other cubes. I've removed the box collider from the starting cube ("marker") and checked the "Is Trigger" box in the destination cube's Box Collider.

    I'm new to Unity myself. I got the first part of the code courtesy of terransage. Have a look at that thread at http://forum.unity3d.com/viewtopic.php?t=6240&highlight=drag+drop
     
  5. rmlloyd

    rmlloyd

    Joined:
    Jul 22, 2008
    Posts:
    82
    hi, thanks.

    I have tried the code above and cannot get it working, every time I drag a cube over another cube which has a trigger then let go, the cube I was holding snaps back into position.

    I have a simple scene; -

    1 cube with the above code attached and NO trigger
    1 cube with a box collider set as a trigger

    Should the target have more information on it? I am guessing from the script above that 'other : collider' means any other object in the scene?

    thanks for your help.
     
  6. giza

    giza

    Joined:
    May 4, 2008
    Posts:
    69
    Hi,

    I've put an example together in a project folder. Just unzip the Drag Drop scene in the Scene sub-folder under the Assets folder and run it. Drag the big cube on the left onto the smaller cube on the right. It'll "stick". On mouse up elsewhere it'll jump back to it's origin. Hope this helps.
     

    Attached Files:

  7. rmlloyd

    rmlloyd

    Joined:
    Jul 22, 2008
    Posts:
    82
    thanks a lot for the example. I have tried it and it works perfectly.

    What I am trying to achieve is a drag 'n' drop game either 2D GUI (pickup) to 3d Drop or better still 3D -> 3D as you have in your example.

    I have tried to add OnMouseOver() detection to the End_Marker but it only works when the movable_Object isn't held. I guess some kind of ray cast could be done to check the mouse is over the object which is below the cube the player is holding. Have you tried anything like this?
     
  8. rmlloyd

    rmlloyd

    Joined:
    Jul 22, 2008
    Posts:
    82
    Hey, I got it working :)

    with the help of several forum searches and the manual.

    however, this works OK for one single cube, I am wondering how easy it would be to expand this to a 100 cubes... I will have a play.

    I have attached the code anyway for your perusal and any ones use.

    Thanks again giza for uploading your project. I really helped me.
     

    Attached Files: