Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by cannedcolors · Nov 12, 2012 at 03:19 PM · gameobjectmouseaxisdrag

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cannedcolors · Nov 19, 2012 at 08:19 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges