• 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
8
Question by Henk Jan Baard · Mar 01, 2010 at 11:32 AM · gameobjectmouse-drag

Drag GameObject with mouse

I have maybe a dumb question, but how do I drag a GameObject when I click the mouse?

I'd like to use OnMouseDrag. When the user clicks on my GameObject it first does some animation (moves forward a bit). When the user still holds down the mouse button and moves the mouse, I'd like to have the GameObject move with it (with dampening).

Tried some stuff out but I can't get it to work.

Does anyone know how to do this? It would be of great help!

Thanks.

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

11 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NicoVar · Aug 18, 2017 at 02:42 AM

Simpler with Unity's new UI:

     public void OnDrag (PointerEventData eventData)
     {
         Vector3 delta = eventData.pointerPressRaycast.worldPosition - eventData.pointerCurrentRaycast.worldPosition;
         delta.z = 0; // this is to keep the camera's Z fixed, change at will
         m_camera.transform.position += delta;
         CheckLimits();
     }

Make sure you add the IBeginDragHandler, IDragHandler, IEndDragHandler interfaces to your MonoBehaviour.

Comment
Add comment · Show 6 · 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 CoreyLyman · Apr 11, 2019 at 09:00 PM 0
Share

Im not using a UI canvas to do this but is there a way to drag and drop an object in the 3d world space? I keep getting the error with not set to an instance of an object on these lines :

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

one under get mouse button one under get mouse button

avatar image Hellium CoreyLyman · Apr 11, 2019 at 09:02 PM 0
Share

$$anonymous$$ost likely because you don't have any active camera tagged $$anonymous$$ainCamera in your scene....

avatar image CoreyLyman · Apr 11, 2019 at 11:01 PM 0
Share

Actually i do but the mouse thing im trying to go for is on a secondary camera thats set over a specific part of the scene thats activated like a $$anonymous$$i game and its in that small area i want the mouse movement

avatar image Hellium CoreyLyman · Apr 12, 2019 at 05:03 AM 0
Share

You have to retrieve somehow a reference to this camera and use it ins$$anonymous$$d of Camera.main.


To get a reference to the camera, either declare it as public Camera $$anonymous$$iniGameCamera ; and drag&drop the camera in the inspector, or call Find + GetComponent


Then :

 Ray ray = $$anonymous$$iniGameCamera.ScreenPointToRay(Input.mousePosition);
avatar image CoreyLyman · Apr 12, 2019 at 08:20 PM 0
Share

I didnt even think about that Thank you so much Hellium much appreciated

avatar image CoreyLyman · Apr 12, 2019 at 08:33 PM 0
Share

Ok so now the issue I am having is its grabbing all the items and not just one at a time.

  • ‹
  • 1
  • 2
  • 3

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set Minimum and Maximum angles on Drag to Rotate Gameobject ?? 0 Answers

Make the gameobject flip to the direction it is being dragged to? 1 Answer

Moving GameObjects with mouse and check contains 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Moving players arms and legs via mouse movement? 0 Answers


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