Search Unity

Mouse Drag

Discussion in 'Scripting' started by nooB, Apr 4, 2010.

  1. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    It would be great if somebody could help me out with this.
    I am attaching a screenshot of the game that I m developing.


    I have to drag the cards out from the GUI and place it on the ground which I have divided into grids.

    1. I would drag the required card on GUI. When mouse exits the GUI and enters the Viewport, I need to instantiate a GameObject at the mouse Position with the same texture overlaid.

    I am not sure how to determine the Mouse Position on exit (in World Coordinates) in the above case

    2. Also, with the mouse pressed I should continue dragging the instantiated card to a desired grid.

    For this I did
    Code (csharp):
    1.  
    2. var cardObj = GameObject.FindWithTag("card");
    3.     screenSpace = Camera.main.WorldToScreenPoint(cardObj.position);
    4.     curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
    5. curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace);
    6.  
    Again the ScreenToWorld conversion works well only for a certain region on the screen. The mouse movement and the object is totally out of sync in other regions.

    If somebody could point out where I m going wrong, it would be very helpful.
     

    Attached Files:

  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
  3. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    What would be the 'z' value in this case?

    I did try using camera.nearplane as z. The cards were totally displaced.