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 DGKN · Feb 21, 2015 at 05:41 PM · c#objectdragsnap

Dragging an object smoothly

Hi,

With the current code below, I can drag and snap an object but the movement is jerky as it calculates round coordinates to snap objects together. How can I make the dragging smoother ? Thanks !

Here's the code :

 void OnMouseDown() 
     {
         scanPos = gameObject.transform.position;
         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
         offset = scanPos - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     }
 
     void OnMouseDrag() 
     {
         curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         curPosition.x = (float)(Mathf.Round(curPosition.x / gridSize_x) * gridSize_x);
         curPosition.y = (float)(Mathf.Round(curPosition.y / gridSize_y) * gridSize_y);
         curPosition.z = (float)(Mathf.Round(curPosition.z / gridSize_z) * gridSize_z);
         transform.position = curPosition;
 
     }
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 DGKN · Feb 21, 2015 at 07:13 PM 0
Share

What I did so far :

I can smoothly move the object around and snap it with another object if I press right mouse click. What I'd like to do is snap it automatically without user input.

     void On$$anonymous$$ouseDrag() 
         {
             curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
             curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
             transform.position = curPosition;
             //Snap mode ON
             if (Input.Get$$anonymous$$ouseButton(1)) {
                 curPosition.x = (float)($$anonymous$$athf.Round(curPosition.x / gridSize_x) * gridSize_x);
                 curPosition.y = (float)($$anonymous$$athf.Round(curPosition.y / gridSize_y) * gridSize_y);
                 curPosition.z = (float)($$anonymous$$athf.Round(curPosition.z / gridSize_z) * gridSize_z);
                 transform.position = curPosition;
             }
     
     
         }


2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Mmmpies · Feb 21, 2015 at 05:53 PM

Don't know if this helps but I put together a basic Kerbal Space Program test a while ago. Scripts are in the package:

SnapDrag

Might be some unused scripts in there as well and as ever, it was OK when it left me but virus check it as it's been on the Internet.

EDIT

Ah OK, the DragWithMouse script is years old, it doesn't do snapping but will drag something smoothly.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(MeshCollider))]
 
 public class DragWithMouse : MonoBehaviour 
 {
     
     private Vector3 screenPoint;
     private Vector3 offset;
     public bool IsDragable = true;
     
     void OnMouseDown()
     {
         if(IsDragable)    // Only do if IsDraggable == true
         {
             screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
         
             offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         }
     }
     
     void OnMouseDrag()
     {
         if(IsDragable)    // Only do if IsDraggable == true
         {
             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 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 DGKN · Feb 21, 2015 at 05:59 PM 0
Share

Thanks I've checked it but I got loads of errors so I removed it. But thanks anyway :)

avatar image Mmmpies · Feb 21, 2015 at 06:05 PM 0
Share

Odd I opened it into a new project and got no errors at all. Opened the scene and dragged the objects around to make sure it worked before posting.

avatar image DGKN · Feb 21, 2015 at 06:17 PM 0
Share

I'm probably using an older version of Unity without the new UI system. That's why I'm getting those errors. But I've managed to open the scene and it looks really good. Thanks for sharing this :)

avatar image Mmmpies · Feb 21, 2015 at 07:00 PM 0
Share

I converted that to comment as it doesn't appear to be an answer, I hope that's O$$anonymous$$. Right I think I've taken out everything that would have caused errors so try this:

SnapDrag

It uses offset colliders, when something snaps to the bottom it moves the collider to below that item.

avatar image DGKN · Feb 21, 2015 at 07:16 PM 0
Share

It still gives me errors when I snap the second object to the third, etc.

Show more comments
avatar image
1

Answer by Khoray · Apr 22, 2017 at 10:07 PM

You can try this also

 using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class DragObject : MonoBehaviour {
     
         private Vector3 screenPoint;
         private Vector3 offset;
     
         void OnMouseDown(){
             screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
             offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         }
     
         void OnMouseDrag(){
             Vector3 cursorPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
             Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
             transform.position = cursorPosition;
         }
     }

  




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

22 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

Related Questions

Distribute terrain in zones 3 Answers

Raycast Snapping Problem. 0 Answers

Multiple Cars not working 1 Answer

Item pickup and drop function 2 Answers

Snap object rotation 1 Answer


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