• 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 Der_Kevin · Apr 20, 2016 at 08:21 AM · onmousedown

void OnMouseDown for mobile?

hey! i wanted to use t$$anonymous$$s snippet: http://pastebin.com/Mn6sx1MC but somehow it doesent work on a phone - but perfect in a editor. i t$$anonymous$$nk that OnMouseDown at line 30 is causing the problem. so t$$anonymous$$s line:

 void OnMouseDown(){
         beingDragged = true;
     }
     void OnMouseUp(){
         beingDragged = false;
         transform.position = oldPosition; // place on the last valid position
         _intersecting = 0; // stationary block do not intersect anymore
         TintRed(_intersecting); // set the tint one last time
     }

how do i have to change t$$anonymous$$s to make t$$anonymous$$s work on a phone?

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

6 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by incorrect · Apr 20, 2016 at 12:59 PM

OnMouseDown() is an old way of interaction. There is no mouse on mobile platform so interactions are handled in a different way. That's why it works in Editor (on PC with a mouse) and does not on mobile.

In Unity 4.6 we got new UI system as well as new Event System.

So to ac$$anonymous$$eve what you want you should have Event System in your scene that will send interaction events. And your interactable objects should have some way of receiving those events.

For a purposes of interaction with GameObjects you can make them have colliders (2d or 3d) and event handler components: either default EventTrigger or your own script that implement Supported Events handling.

Also you need to add a Raycaster of proper type to your camera that will send those events.

In your case the old OnMouseDown() method will be replaced by new OnPointerDown() wich works on both PC and mobile platforms.

Hope t$$anonymous$$s will help you to get the idea of interaction in Unity and solve your problem. If you have any questions left, feel free to ask.

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
1

Answer by mostwanted9192 · Apr 20, 2016 at 09:17 AM

OnMouseDown() is not available on devices because of Touch implementation. For further answer, Kindly look t$$anonymous$$s answer. http://answers.unity3d.com/questions/124198/onmousedown-alternative-for-mobile.html

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

Answer by aditya · Apr 20, 2016 at 10:32 AM

as Phones and tablets don't have any MOUSES then OnMouseUp or OnMouseDown will not work on them .... try t$$anonymous$$s

 void Update(){
             if (Input.touchCount == 1) {
                 if(Input.GetTouch(0).phase == TouchPhase.Began)
                     beingDragged = true;
                 if(Input.GetTouch(0).phase == TouchPhase.Ended){
                     beingDragged = false;
                     transform.position = oldPosition;
                     _intersecting = 0;
                     TintRed (_intersecting);
                 {
             }
 }

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 hendryshaikh2004 · Mar 10, 2021 at 11:56 AM 0
Share

It Worked Thanks

avatar image
0

Answer by Nikunj-Kareliya · Apr 20, 2016 at 09:01 AM

Other alternative is to use methods of Input class inside the Update().

 void Update () {
         if(Input.GetMouseButtonDown(0)) {
             beingDragged = true;    
         }
         else if (Input.GetMouseButtonUp (0)) {
             beingDragged = false;
             transform.position = oldPosition; // place on the last valid position
             _intersecting = 0; // stationary block do not intersect anymore
             TintRed(_intersecting); // set the tint one last time
         }
 
 }

Comment
Add comment · Show 2 · 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 Der_Kevin · Apr 20, 2016 at 09:24 AM 0
Share

ok, thanks. that sounds pretty reasonable. but somehow my drag function is acting weird now: http://imgur.com/a/cQGpU

the second gif is how it was before - but not working on mobile. the first how it is after the code changes

avatar image Nikunj-Kareliya · Apr 29, 2016 at 01:01 PM 0
Share
 // Suppsoe it is perspective camera view
     void Update () {
         if (Input.GetMouseButton(0)) {
                             
             // Convert mouse position into world position
             // make sure you have passed the distance between object and camera as a 3rd parameter of vector3() method
             Vector3 worldPos = mainCamera.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20f));
 
             Vector3 tmpPos = cloneBasket.transform.position;
             // Drag only in X axis
             tmpPos.x = Mathf.MoveTowards (tmpPos.x, worldPos.x, Time.deltaTime * 5);
             // Restriction of object in X axis
             tmpPos.x = Mathf.Clamp (tmpPos.x, 1502.8f, 1507);
             cloneBasket.transform.position = tmpPos;
         }
     }
avatar image
0

Answer by meat5000 · Apr 20, 2016 at 10:42 AM

The modern Unity 5 way is to disregard the concept of Mouse and Touch and move to the concept of 'Pointers'. The Standalone Input Module (Touch Input Module now deprecated) has all the methods to handle events from multiple Pointers, w$$anonymous$$ch include both cursor and touch input.

http://docs.unity3d.com/530/Documentation/ScriptReference/EventSystems.StandaloneInputModule.html

The Standalone Input Module is designed to work with new UI through the Event System.

To implement Dragging you can use OnDrag

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
  • 1
  • 2
  • ›

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

48 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

Related Questions

OnMouseDown with 360 controller unity? 1 Answer

GameObject Array 1 Answer

Increase mouseclick area size for OnMouseDown() - For Mouse Shooting Game (Open to more ideas) 2 Answers

Click on a gameObject to show/hide a GUI box help C# 1 Answer

Collider priority with onMouseDown() 2 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