• 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
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
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
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
0

Answer by sirbluenotez · Jun 06, 2011 at 02:10 AM

This is the simpliest way i can think of:

 public class DragMove : MonoBehaviour
 {
     void OnMouseDrag()
     {
         Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,(transform.position.z-Camera.main.transform.position.z)));
         point.z = transform.position.z;
         transform.position = point;
     }
 }
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 nejisk · Jan 11, 2013 at 02:00 PM

the code on the previous page, it is moving the object from the angle of camera, I need the version which moves the object in respect to planes, from cameras angle [ for example from the cameras angle if i move the object it moves on the direction of y, but i want it to move on the direction of x , or z because its my plane, alltough the object moves on x, and partial y and partial z because of my camera angle how can i block it from moving on y direction and make it move on z only?

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