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
0
Question by ardizzle · Jul 23, 2013 at 12:23 AM · dragmouse-drag

How to click and drag an object at full speed

I can click and drag my objects like I want but if I move my mouse to fast I lose the object and then I have to go click it again to drag it. How can I make sure my object stays with my mouse? Here is my current code;

 if(Input.GetMouseButton(0))
     {
         var hit : RaycastHit;
         var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition); 
         if(Physics.Raycast(ray,hit))
         {
             if(hit.transform.gameObject == selectedObject)
             {
                 selectedObject.transform.position.x = hit.point.x;
                 selectedObject.transform.position.y = hit.point.y;
             }
         
         }
 
     }
Comment
Add comment · Show 1
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 pickle chips · Jul 25, 2013 at 01:45 AM 0
Share

Have you considered using On$$anonymous$$ouseDrag() ?

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseDrag.html

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Jul 23, 2013 at 07:18 AM

Your problem is happening because the mouse is getting ahead of the object. When that happens, the Raycast() fails, so the drag stops. The only way I can think of to fix this problem is to change it so that the initial raycast starts the dragging, but after that it uses something else to move the cube. Here is my quick take on fixing the problem. The code got a whole lot more complicated, but I could not think of a simple fix:

 private var dist : float;
 private var toDrag : Transform;
 private var dragging = false;
 private var offset : Vector3;
 
 function Update() {
     if(Input.GetMouseButtonDown(0))
     {
         var hit : RaycastHit;
         var v3 : Vector3;
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
         if(Physics.Raycast(ray,hit))
         {
            if(hit.transform.gameObject == selectedObject)
             {
                  toDrag = hit.transform;
                  dist = hit.transform.position.z - Camera.main.transform.position.z;
                  v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
                  v3 = Camera.main.ScreenToWorldPoint(v3);
                  offset = toDrag.position - v3;
                  dragging = true;
             }
         }
     }
     if (Input.GetMouseButton(0))
     {
         if (dragging)
         {
             v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
             v3 = Camera.main.ScreenToWorldPoint(v3);
             toDrag.position = v3 + offset;
         }
     }
     if (Input.GetMouseButtonUp(0))
     {
         dragging = false;
     }
 }
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 ardizzle · Jul 25, 2013 at 01:30 AM 0
Share

It works almost perfectly. The only problem is its inverted. if i move my mouse up my part moves down. and vice versa. I tried playing with the code and i can't fix. Any idea? And thanks by the way you helped me out a ton man.

avatar image robertbu · Jul 25, 2013 at 01:41 AM 0
Share

I'll bet you have your camera at positive 'z' looking back at negative 'z'. You can fix it in a couple of ways. Change line 17 to:

 dist = $$anonymous$$athf.Abs(hit.transform.position.z - Camera.main.transform.position.z);

or

 dist = Camera.main.transform.position.z - hit.transform.position.z;


The issue is that Camera.main.ScreenToWorldPoint() must get the positive distance in front of the camera, and the initial calculation was returning a negative distance for a camera that was not in the default position/orientation.

avatar image ardizzle · Jul 26, 2013 at 02:24 AM 0
Share

Thanks a ton man. It works perfect. I learned a lot right here. Thanks again :)

avatar image Finnigan · Nov 20, 2014 at 03:33 PM 0
Share

This has been really useful, thank you both. However, why does it drag slower when the camera is on the side ins$$anonymous$$d of the front or the back of the object?

avatar image abragon · Jan 11, 2015 at 10:30 AM 0
Share

Thanks alot, I was searching for a while now. I was wondering, how you can reduce the dragspeed?

Show more comments
avatar image
0

Answer by Tsilliev · Mar 31, 2018 at 07:28 AM

If your mouse pointer is getting ahead of the object thus losing the collider of the object here is an easy solution without offsets. The solution is that on mouse button down, store the last clicked object, and on mousebutton (constant execution) drag the object.

 GameObject DraggedObj;
 
 void Update()
 {
 
 if (Input.GetMouseButtonDown (0))
             {
                     RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
                     if(hit.collider != null)
                     {
                         if (hit.transform.gameObject.tag == "Card")
                         {
                         DraggedObj = hit.transform.gameObject;
                         } 
                     } 
 
             }
 
                 if (Input.GetMouseButton(0))
                 {
                     if(DraggedObj != null)
                     {
                         Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
                         point.z = DraggedObj.transform.position.z;
 
                         DraggedObj.transform.position = point;
                     
                     }
                 
                 }
                 else if (Input.GetMouseButtonUp(0))
                 {
 
                 OnMouseUp ();
                 DraggedObj = null;
                 }
 }


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

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to drag a GameObject with touch but at a limited speed 1 Answer

How will i drag my object only x and y axis? 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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