Search Unity

How to pull an object with mouse... ?

Discussion in 'Scripting' started by Erguntech, Apr 6, 2011.

  1. Erguntech

    Erguntech

    Joined:
    Feb 21, 2011
    Posts:
    11
    Hi all ,
    What is the way to make an object dynamicly effected by mouse ?

    For example, i have a cabinet and which has a drawer.

    I want to pull the drawer with mouse,and also be able to close it back. I can maybe trigger an animation by clicking on the drawer but what i want to reach is to make a drawer,which can be pulled by mouse..
    Is there a easy way to do this whitout scripting maybe ?

    Cheers..
     
  2. Satoh

    Satoh

    Joined:
    Feb 17, 2011
    Posts:
    56
    without scripting, I highly doubt it.

    However, I don't think it would be difficult to make a script to open or close the drawer. I haven't dealt much with the mouse yet, but I think I understand the components of the script you will need.

    Someone else who knows more about Unity scripts should be able to figure out what I mean, and hopefully help you out.

    if : mouse button is clicked : send a raycast from the mouse screen position straight forward
    ...if the raycast hits the drawer :
    ......if the drawer is not fully open and the mouse is moved DOWN : move the drawer object in an outward direction
    ......else if the drawer is not fully closed and the mouse is moved UP : move the drawer object in an inward direction


    hope that makes sense. I don't know the actual code to do this, but I wouldn't imagine it should be hard to write. (I haven't used raycasts yet, but they seem to solve most of the issues I see on this board XD )


    EDIT:

    This may help you out a bit.
    http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDrag.html
     
    Last edited: Apr 7, 2011