- Home /
Drag GameObject only in a specific area
Hi there!
I'm kinda new to Unity (but used to programming with javascript) and got the following problem:
I want to use a interactive tool where the user can operate technical devices via drag and drop. For example grabbing a robot arm and dragging it along its rail (Dragging is needed, can't use GUI Elements).
I found billions of scripts for dragging around GameObjects in Unity but the following problems remains and I really hope some got a easy solution:
I need to choose whether the object can be moved on X-Axis, Y-Axis or both
The object "jumps" to where the cursor is pointing when it's clicked
The object must only be moved along it's rail, so how far I can drag it must me limited somehow.
Additional Information: The script is implemented into my main camera and the objects which are able to be dragged are selected via a "dragable"-tag.
Sorry for asking such basic stuff but I'd be really grateful for anyone to help me!
Greets, Robin
Answer by FakeBerenger · Nov 12, 2012 at 03:34 PM
Create an enum and check it when you move. If it's Y_only, for instance, then do newPos.x = oldPos.x;
When you click on a collider, you can get the impact point ray / collider. You just have to set the position of the object to that point.
For the last one, I suggest you find/write a function that gives you the closest image of a point on a line, then create a line (your rail) and finally affect that point to the transform each frame.
Thanks for the help! I was told to define a "range" where the Object can be moved. I tried to check if the object is leaving the range and if it does, it can't be moved in that direction any more.
Of course the GO will get stuck now if it leaves the range. Is there a way to say "if object leaves the range it can only be dragged in ONE direction on the specified axis"? So the object can be moved back in range but not out of it.
Your answer
Follow this Question
Related Questions
Fixing "Drag GameObject with mouse" script 1 Answer
Dragging a GameObject with mouse 1 Answer
Controlling an animation by dragging a gameObject with the mouse 0 Answers
Resize / Scale, Drag, Move a Plane at Runtime by Mouse action 2 Answers
On Mouse Drag Game Object follows Mouse on a single Axis. 1 Answer