Search Unity

Dragging Objects with Mouse

Discussion in 'Scripting' started by gilson, Mar 29, 2011.

  1. gilson

    gilson

    Joined:
    Sep 13, 2010
    Posts:
    36
    Hi everyone, im trying to make a script that when i click on a mouse with the mouse, the user will be able do drag it around while he holds the button.

    Here is my piece of code:

    Code (csharp):
    1.  
    2. function Update () {
    3.     if (Input.GetMouseButton(0)) {
    4.         var hit : RaycastHit;
    5.        
    6.         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    7.    
    8.         if (Physics.Raycast(ray, hit)) {           
    9.             mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);          
    10.             hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
    11.         }
    12.     }
    13. }
    14.  
    I'm having some problems, fist problem:

    I'm setting a fixed Z to the mouse position and i want the object to stands in its original z axis. Any insights about that? I've tried some other things but it didnt work properly.

    Second problem:
    I want the first click to doesnt move the object, the first just to select and after the click you move the mouse and then the object comes along. This way, any click the objects moves a little.

    Thank you,
     
  2. gilson

    gilson

    Joined:
    Sep 13, 2010
    Posts:
    36
  3. MADmarine

    MADmarine

    Joined:
    Aug 31, 2010
    Posts:
    627
    Well for problem 1, why not just use the Z position of the object when assigning mousePos?

    mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, hit.transform.position.z);

    As for problem 2, you should have a Transform variable for the object selected, let's call it m_SelectedObject, on raycast hit, the hit.transform should be assigned to m_SelectedObject. If the raycast does not return a hit, assign m_SelectedObject to null.

    Then in the update function outside of the raycast code, have this:

    Code (csharp):
    1.  
    2. if(m_SelectedObject != null)
    3. {
    4.         mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, m_SelectedObject.transform.position.z);   
    5.         m_SelectedObject.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
    6. }
    7.  
    So basically you check whether you have anything selected, if you do move it to the position of the mouse (while maintaining its original Z position)
     
  4. gilson

    gilson

    Joined:
    Sep 13, 2010
    Posts:
    36
    I cant use like this:
    Code (csharp):
    1.  
    2. mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, hit.transform.position.z);
    3.  
    Because afterwards i do this:
    Code (csharp):
    1.  
    2. hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
    3.  
    And it screw things up bringing the object to the camera Z position.

    Any ideias?
     
  5. gilson

    gilson

    Joined:
    Sep 13, 2010
    Posts:
    36
    I've noticed that on the mousePos.z i have tu put the difference between my camera.z and my object.z and it works :)

    Like this:

    Code (csharp):
    1.  
    2. var desiredZ: float = Mathf.Abs(Utils.Round((Camera.main.transform.position.z - hit.transform.position.z),2));
    3. mousePos = Vector3(Input.mousePosition.x, Input.mousePosition.y, desiredZ);
    4. hit.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
    5.  
    It works, but does anyone have a more ellegant solution?
     
  6. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,370
    Camera.main.ScreenToWorldPoint() needs a camera relative z position does it not? If you just give it the Z position 0, then it will return the position fo the camera. The Z position you want to use is the distance between m_SelectedObject and the camera.

    Assuming the camera is looking along the z axis.
    Code (csharp):
    1. m_SelectedObject.transform.position = Camera.main.ScreenToWorldPoint(mousePos.x, mousePos.y, m_SelectedObject.transform.position.z - Camera.main.transform.position.z);
    edit:
    Beat me to it Gilson!
     
    Last edited: Mar 30, 2011
  7. Satoh

    Satoh

    Joined:
    Feb 17, 2011
    Posts:
    56
    This thread pertains to my interests.

    On the note of objects being selected, just add a selectedObject variable, and


    if(selectedObject != hit.raycast.IforgotwhateverXD)
    {selectedObject = hit.raycast.bluh;
    /*test for mouse release before continuing*/}
    else
    {/*your already written logic here*/}

    Sorry if this doesn't help, but in any case I'm trying to do something similar, or rather I will be in the near future.
     
unityunity