• 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
2
Question by HauntedHotPocket · Jun 29, 2015 at 07:39 AM · drag-and-drop

Drag object by point clicked vs transform center

Hi, I'm moving an object via click/drag, but it instantly moves the objects transform center point to the hit.point position. What I'd like it to do is move the object relative to the point I actually clicked on it. ex: If I click on the corner, let the mouse continue moving from the corner of the object, not suddenly snap to the center of the object. Any idea what I'm doing wrong?

       ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          if(Physics.Raycast(ray, hit) && hit.transform == this.transform)
          {
              this.transform.position.x = hit.point.x;
              this.transform.position.z = hit.point.z;
          }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jul 24, 2015 at 06:46 AM

That's quite simple. All you have to do is store the offset from the objects center to the initial hitpoint when you start dragging. When you set the object to it's new position you simply revert the offset.

However your code has another problem. You get the new position by raycasting onto the object itself. Depending on the objects shape that might have strange results. Another way is to store the distance of the initial hit as well and simply place the object at this distance from the camera. If you use an orthographic camera the depth of course doesn't matter as long as the camera is aligned with the world z-axis.

I guess you use UnityScript? (guess based on your "hit" variable)

 // UnityScript
 private var dragging = false;
 private var dragOffset : Vector3;
 private var dragDistance : float;
 
 function Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         var hit : RaycastHit;
         if(Physics.Raycast(ray, hit) && hit.transform == transform)
         {
             dragging = true;
             dragDistance = hit.distance;
             dragOffset = transform.position - hit.point;
         }
     }
     if (Input.GetMouseButton(0) && dragging)
     {
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         transform.position = ray.GetPoint(dragDistance) + dragOffset;
     }
     else
     {
         dragging = false;
     }
 }

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 Bunny83 · Jul 24, 2015 at 06:52 AM 0
Share

Note: When using a perspective camera this would "drag" the object on a spherical path around the camera (due to the perspective projection of the ray). For perspective cameras it's usually the best approach to use a Plane in world space to raycast against to get the reference point for dragging.

For orthographic cameras this should work just fine.

avatar image
1

Answer by hawken · Nov 09, 2015 at 01:06 PM

Here's a stripped down C# version if you are just dealing with mouse and 2D

     Vector3 mousepos;
     Vector3 clickedPos;
     Vector3 offSet;
    
     void OnMouseDown() {
         clickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         offSet = transform.position - clickedPos;
 
     }
 
     void OnMouseDrag() {
         mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         transform.position = new Vector3(Mathf.Round(mousepos.x), Mathf.Round(mousepos.y), 0) + offSet;
     }
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 QuadomSoft · May 12, 2016 at 11:46 AM 0
Share

Awesome, thanks!

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

Drag and drop 3d game 1 Answer

Dragging an object in iOS (UnityScript) 3 Answers

how can i put just one child in a drag and drop container in ngui 1 Answer

How to create Arrows Between Two objects and Animate them 2 Answers

How do I detect OnDrop event on a GameObject? 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