Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Xenocide · Dec 12, 2016 at 02:44 AM · inputtouchcursorpointonmousedrag

Is there a way to ensure a cursor position must hover over an object to work in OnMouseDrag?

I never really know how to word these questions, because there's always more questions than one.

Essentially, I have an object I move up and down using this code:

     private Vector3 offset;
     private bool stopDrag = false;
     private bool twoClicks;
     //private int fingerCount;
 
     void Start()
     {
         Input.multiTouchEnabled = false;
     }
 
     void Update()
     {
         if (Input.touchCount > 1)
         {
             stopDrag = true;
         }
 
         //if (fingerCount > 0)
             //print("User has " + fingerCount + " finger(s) touching the screen");
 
     }
 
     /***Slider***/
     void OnMouseDown(){
 
         stopDrag = false;
         //screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(1, Input.mousePosition.y));
     }
 
     void OnMouseDrag(){
 
         /*int fingerCount = 0;
         foreach (Touch touch in Input.touches)
         {
             if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                 fingerCount++;
 
         }*/
         if (Input.touchCount == 1 && stopDrag == false)
         {
             Vector3 cursorPoint = new Vector3(1, Input.mousePosition.y);
             Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
             transform.position = cursorPosition;
 
             /***Lock down slider***/
             Vector3 pos = transform.position;
             pos.y = Mathf.Clamp(pos.y, -4, 4);
             transform.position = pos;
         }
         if (stopDrag == true)
         {
             return;
         }
     }

So I'm able to move the object up and down where the cursor position is, but if I do a second touch (say to press a button) and then take my finger off the object with the second touch held down, the object will jump to the cursor position (within the 'clamped' Y axis).

Does anyone know of a way to stop that from happening? I've tried adding a bool that turns true when more than one touch is detected, but I also want to be able to move the object while I'm making another touch on something else. I guess I want to have my cake and eat it too. Meaning, it's either one or the other as far as I'm aware.

All I can think of is to make it so the OnMouseDrag can only work if the cursor is hovering over the object, but I'm not sure how I'd achieve that.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xenocide · Dec 12, 2016 at 12:32 PM

Ignore this. Made an edit below*I fixed the issue I was having by adding this code to the script:

     void OnMouseOver()
     {
         if (GameObject.FindWithTag("Player"))
         {
             stopDrag = false;
         }
     }
 
     void OnMouseExit()
     {
         stopDrag = true;
     }

I can now have my cake and eat it too! Hurrah. I always end up fixing the problem before anyone can help me. But hopefully this helps anyone else with any problems/bugs they've encountered when it comes to dragging an object. Input.touchCount has been very helpful.

*Edit: Actually, it's not what I want. Because you can still jump the object's position like before, you're just confined to the game object's hover space. Also, it's easier to lose the drag of the object because when you drag your finger away from the object the dragging stops.

I guess what I really want is to be able to click and drag the object anywhere on the screen, but to also stop it from jumping to the cursor's position when I touch somewhere else and lift off the first touch.

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

100 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Touch Controls without virtual joystick 0 Answers

Moving Unity DualTouchControls/Joystick on touch but also functioning on initial touch. 0 Answers

Non-responsive touch controls after showing ads. 0 Answers

How to know know when user has stopped dragging 1 Answer

Sprite/gameObject touch or click event 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