- Home /
Mouse drag and drop a gameobject javascript
Hi,
I'm searching for a drag and drop script to use on a gameobject, the only thing I founded is to use the mousedrag function. I'm a noob in unity as you can hear but plz answer. I prefer javascrip!
Bye
Answer by Berenger · Mar 08, 2012 at 12:26 AM
I'm not much of the giving-script kind of guy (and I don't have one anyway), so here are guidelines :
You want to detect three events, the mouse down (start drag) pressed (dragging) and up (stop drag).
You can either create a script containing the functions OnMouseDown, OnMouseDrag and OnMouseUp and add it to the draggable objects, or do it yourself with ScreenToPointRay, Physics.Raycast and Input.GetMouseButton.
You are now dragging, you need to move the object. You need the position of the mouse in 3D, and it depends of the plane you want to use. Here, you can use either a plane with a collider, or a Plane object, and once again cast a ray. Finally, move the object to that point.
Good luck !
Answer by bertvdb · Mar 08, 2012 at 04:41 PM
Ok if I use this 3 function it works but the object stays in the middle of the screen when the mouse is in the down left corner of the screen. Do you have any idea how to fix this thing?
Bert