Mouse Dragging

I am perplexed at how to detect if the mouse is being dragged and in what direction. I am working on a script where a player can grab an object and move it by dragging the mouse. I looked all through the unity scripting guide and found things like event.delta and mouse drag but I didn’t see any examples of how to implement these classes or any sample code. Has anyone done this before.

Would you detect the mouse drag in the update code or in it’s own function. I am still getting use to Unity scripting.

Thank you

Add a Collider to your object. Attach a script which inherits/extends the MonoBehaviour class to you object. Your object will then receive OnMouse* events, including one called OnMouseDrag.

In the OnMouseDrag event, get the current mouse position, convert the mouse position from screen space to world space, then move the object to the computed world space position.

Take care with the z-axis value of the world space position as, I believe, it will be a constant value.

Okay I just don’t understand the syntax and couldn’t figure it out from the script guide. Is on Mouse drag a function? Do I say something like on mouse drag return position or something. Maybe I don’t know how to read the script guide because all I saw was that there was an on mouse drag but I don’t get how to use it in a script.

to find the mouses position on the screen you could use something like this:

But if you’re just trying to find the direction and speed of mouse movement you could use Input.GetAxis(“Horizontal”) and Vertical (Presuming you haven’t changed the Input keys)

You should be able to find any further info in the scripting reference:

Actually, “Horizontal” and “Vertical” are keyboard / joystick / gamepad inputs. What you want to use are “Mouse X” and “Mouse Y” (assuming the default input manager settings).