• 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 JAHBLO · Sep 04, 2014 at 11:43 AM · drag

Drag objects not working properly

Hi guys, I am trying to do the same thing as this post but here is not working that well, I am able to drag the object by touching and dragging, but it won't hold for long, after a few milliseconds it falls Oo

I have tried many different approaches, same results so far, any help will be appreciated.

I am pretty new in Unity, so that might be the problem :/

My code:

    void Update(){            
         if(Input.touchCount > 0) {
              if(Input.GetTouch(0).phase == TouchPhase.Moved){
                 checkTouch(Input.GetTouch(0).position);
              }
         }
    }       
    void checkTouch(Vector3 position){
 
         Vector3 wp = Camera.main.ScreenToWorldPoint(position);
         Vector2 touchPos = new Vector2(wp.x, wp.y);
         var hit = Physics2D.OverlapPoint(touchPos);
         
         if(hit){
             if (GameObject.Find(hit.transform.gameObject.name) != null){
 
                 //GameObject tObject = GameObject.Find(hit.transform.gameObject.name);
 
                 rigidbody2D.isKinematic = false;
                                 
                 Vector3 touchMove = Camera.main.ScreenToWorldPoint(new Vector3 (position.x, position.y, distance));//curScreenPoint
 
                 transform.position = touchMove;//curPosition
                 
                 // make the camera stop moving while dragging                
                 cam.GetComponent<MouseCamera>().enabled = false;    
             }
         }else{
             //enable camera movement     
             cam.GetComponent<MouseCamera>().enabled = true;    
         }
     }
 

Comment
Add comment · Show 6
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 Mayank Ghanshala · Sep 04, 2014 at 12:04 PM 0
Share

change if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) to if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved||Input.GetTouch(0).phase == TouchPhase.Stationary)

avatar image JAHBLO · Sep 04, 2014 at 12:17 PM 0
Share

Thank you so much for the reply, but it is not that..still behaving in the same way, after while the object just don't follow the finger :/

avatar image Mayank Ghanshala · Sep 04, 2014 at 12:32 PM 0
Share

ohk I got that you have to zero your velocity or 0 your gravity when you doing dragging like rigidbody2D.is$$anonymous$$inematic = false;rigidbody2D.velocity = new Vector2(0,0); something like that I think your obj is getting velocity from gravity

avatar image JAHBLO · Sep 04, 2014 at 12:57 PM 0
Share

It is still not that, buddy.

The interesting thing is if I ignore the "hit" validation like that:

 if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved || Input.GetTouch(0).phase == TouchPhase.Stationary){
                     //checkTouch(Input.GetTouch(0).position);
                     Vector3 touch$$anonymous$$ove = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);//curScreenPoint
                     touch$$anonymous$$ove.z = 0;
                     transform.position = touch$$anonymous$$ove;
 
                 }

It works perfectly, but I need the hit validation, otherwise it will drag all objects nearby.

avatar image Mayank Ghanshala · Sep 04, 2014 at 01:00 PM 0
Share

if it is workin use hit and dont do rigidbody2D.is$$anonymous$$inematic = false;

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why is there still drag when drag is 0? 0 Answers

OnDrag - Object lagging behind the mouse cursor. 0 Answers

Nested Scroll Views: How can I pass drag control from an inner scroll view to its outer scroll view? 2 Answers

How to use gameObject as a drag-able object. 1 Answer

UI drag and drop 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