• 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 Henk Jan Baard · Aug 05, 2010 at 06:31 AM

"Sorry....what's "scanPos" of the code?"

scanPos is a the position (Vector3) of the gameobject you want to drag. In my case it is a scanner so scanPos stands for scanner position :-)

Comment
Add comment · 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
1

Answer by Franz · Aug 04, 2010 at 04:06 PM

Sorry....what's "scanPos" of the code?

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 MorphVGX · Jul 02, 2014 at 05:54 PM 1
Share

The complete and C# version of it:

     public Camera $$anonymous$$ainCamera;
 
     private BoxCollider _collider;
 
     private Vector3 _offset;
 
     private Vector3 _screenPoint;
     private Transform _cachedTransform;
 
     virtual public void Awake()
     {
         _collider = GetComponent<BoxCollider>();
         _cachedTransform = transform;
     }
 
     public void EditorEnable(bool enabled)
     {
         _collider.enabled = enabled;
     }
 
     void On$$anonymous$$ouseDown()
     {
         Vector3 pos = _cachedTransform.position;
 
         _screenPoint = $$anonymous$$ainCamera.WorldToScreenPoint(pos);
         _offset = pos - $$anonymous$$ainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, _screenPoint.z));
     }
     
     
     void On$$anonymous$$ouseDrag()
     {
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _screenPoint.z);
         
         Vector3 curPosition = $$anonymous$$ainCamera.ScreenToWorldPoint(curScreenPoint) + _offset;
         _cachedTransform.position = curPosition;
     }

avatar image
19

Answer by Henk Jan Baard · Mar 02, 2010 at 07:58 AM

Thanks for your reactions. This is what I did to make it work:

void OnMouseDown() { screenPoint = Camera.main.WorldToScreenPoint(scanPos);

 offset = scanPos - Camera.main.ScreenToWorldPoint(
     new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

}

void OnMouseDrag() { Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

 Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
 transform.position = curPosition;

}

Comment
Add comment · Show 5 · 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 Lipis · Mar 02, 2010 at 11:18 AM 1
Share

It's not moving really natural I would say.. and I've notice that a lot of people here.. answering their own questions here.. It's not a forum like site.. check the stackoverflow.com :) Usually you are commenting on someones question.. (to get more details on a specific matter) or just updating the main question.. don't forget that there is an edit here.. Regards

avatar image atr0phy · Mar 29, 2015 at 06:32 AM 1
Share

@Lipis There is nothing wrong with that, and since you're familiar with StackOverflow, you should know there are plenty of examples of people answering their own questions. In fact, it's even encouraged to provide both a question and answer to subjects that a user knows well. Just because he answered his own question doesn't mean nobody else should answer. Especially if they have a better solution. That's what makes communities like this so useful.

avatar image shaneparsons · Jul 11, 2016 at 04:41 PM 0
Share

Your code block is broken, you should fix it to benefit future readers.

avatar image brunocoimbra shaneparsons · Jul 11, 2016 at 05:04 PM 1
Share

@shaneparsons And you should not post on something already answered from 2010.....

avatar image Owen-Reynolds shaneparsons · Jul 11, 2016 at 07:23 PM 2
Share

Commenting on old Qs is fine. The trick is that the Q's aren't really for the person asking -- shanep's "future readers" phrase is correct. Typical comments are fixes for new version of Unity, or, like this, other things that have since broken.

But it's more common to have a lighter touch, for example "Looks like the code formatting is broken. Could anyone fix it?"

And for this Q, another trick is that the accepted answer isn't especially the best. There are several nicer version of this, below, and one even has more checks. Hopefully, users know enough to scroll down, so don't really need this one fixed.

avatar image
24

Answer by Lipis · Mar 01, 2010 at 03:33 PM

There is a script which comes with Standard Assets of Unity called DragRigidbody.js. You can play with it by following these steps:

  • Create an empty Scene with a Terrain
  • Add some GameObject like Cube and Sphere
  • Assign Rigidbody to the ones that you want to be able to Drag (Component > Physics > Rigidbody)
  • Add a First Person Controller from the Prefabs of Standard Assets
  • Finally drag the DragRigidbody.js on the First Person Controller object in your scene.
  • Press the Play button and you are good to go

I don't know in how much into details you want to go, but in general the DragRigidbody.js script is using a combination of Raycast, Rigidbody and SpringJoint to achieve the Drag and Drop.

You can also check the Room of Shadows example which is a little bit more complicated but this is because they also highlighting the objects and playing with lights/shadows.

Have fun and good luck.

Comment
Add comment · 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
0

Answer by Ashkan_gc · Mar 01, 2010 at 02:33 PM

there is another script in unity's procedural example project.

Comment
Add comment · 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
  • ‹
  • 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