- Home /
Click and drag an object
Hi,
Is there a simple way to click on an object and drag it somewhere else and then drop it there?
Thanks!! Chris
Answer by burnumd · Apr 04, 2011 at 08:39 PM
The Wiki has a script for dragging objects: http://www.unifycommunity.com/wiki/index.php?title=DragObject
To restrict movement on an axis, examine the line
var move = cam.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, camTransform.position.y - myTransform.position.y)) - myTransform.position;
Below that, zero out the value that you don't want changed. For example, to ensure the object isn't dragged on the z-axis:
move.z = 0;
If you want the script to be more portable, I'd recommend adding a public variable at the beginning:
var dragMultipliers : Vector3 = Vector3.one;
In the inspector, change "Drag Multipliers" values to "0" on axes that shouldn't move. Then, where you would put move.z = 0;
, instead put
move = move * dragMultipliers;
This really helped me do what I wanted to do, but do you know what I have to put in to lock it to 0 on the Z-Axis?
Answer by inbowin · Apr 09, 2012 at 10:37 AM
move = move * dragMultipliers; (cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'UnityEngine.Vector3'. )
Answer by inbowin · Apr 09, 2012 at 10:37 AM
move = move * dragMultipliers; ( cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'UnityEngine.Vector3'. )
Your answer
Follow this Question
Related Questions
Destroy objects by clicking on them 1 Answer
Can I detect detect if I clicked on same spot which is enlighted by spotlight? 0 Answers
Limiting Drag and Drop 1 Answer
Run script when object is clicked 1 Answer
How to attach objects using a mouse click on one side, and object only attaches to that one side? 0 Answers