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 /
avatar image
5
Question by SrBilyon · May 30, 2011 at 11:55 PM · androidmobileclickonmousedown

OnMouseDown alternative for Mobile

As the API states, OnMouseDown doesn't work for Android/iPhone.

I have a script attached to a prefab that destroys the object when clicked on.

 function OnMouseDown () {
     Destroy(gameObject);
 }

Is there a way to easily do this for touch without the typical raycast or screentopoint? If not, how would I be able to implement this the easiest?

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 cregox · Nov 18, 2014 at 07:28 PM 0
Share

At least nowadays I can attest it does work for android, at least. Also, postmum related: http://answers.unity3d.com/questions/716227/onmouse-event-handlers-might-impact-performance-mo.html

5 Replies

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

Answer by macdude2 · May 31, 2011 at 04:44 AM

if you look in the unity wiki, they have a script that converts Onmousedown calls to touch friendly calls. This will allow you to use OnMouseDown without having to write all of the above each time you need to use the function.

http://www.unifycommunity.com/wiki/index.php?title=OnMouseDown

Comment
Add comment · Show 3 · 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 guybrush.threepwood · Nov 02, 2014 at 09:13 AM 3
Share

new URL: http://wiki.unity3d.com/index.php/On$$anonymous$$ouseDown

avatar image cregox · Nov 18, 2014 at 07:27 PM 1
Share

newer script: http://wiki.unity3d.com/index.php/OnTouch

avatar image fantom642 · May 03, 2015 at 10:27 AM 1
Share

The link in your answer doesn't work anymore, could you update it?

avatar image
3

Answer by LawyerOfGod · Jan 01, 2014 at 04:17 PM

today, you can use OnMouse functions on Mobile device....

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 youblistermypaint · Aug 12, 2015 at 03:44 PM 0
Share

These appears to be correct, see http://docs.unity3d.com/ScriptReference/Input-simulate$$anonymous$$ouseWithTouches.html.

avatar image
0

Answer by sneftel · May 31, 2011 at 12:09 AM

Try this in some gameobject (untested):

 void OnUpdate()
 {
     foreach(Touch t in Input.Touches)
     {
         if(t.phase == TouchPhase.Began)
         {
             Vector3 point = new Vector3(t.position.x, t.position.y, 0);
             Ray ray = Camera.main.ViewportPointToRay(point);
             RaycastHit hit;
             if(Physics.Raycast(ray, out hit))
             {
                 hit.collider.SendMessage("OnMouseDown", null, SendMessageOptions.DontRequireReceiver);
             }
         }
     }
 }
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 insominx · Aug 01, 2012 at 10:08 PM 0
Share

I think this needs to be Camera.main.ScreenPointToRay ins$$anonymous$$d of the viewport version of the function.

avatar image keless · Mar 06, 2013 at 07:10 PM 1
Share

also, "Input.touches" with a lower-case 't' on touches

avatar image
0

Answer by SimonDarksideJ · Jul 10, 2014 at 03:32 PM

As of 4.5 you no longer can on some platforms. Confirmed on Windows Phone that the mouse code is GONE.

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
0

Answer by guybrush.threepwood · Nov 02, 2014 at 09:33 AM

Since it is possible to use the OnMouse_ Handler for mobile, is this http://wiki.unity3d.com/index.php/OnMouseDown the same what unity is doing internally? I wonder if it just suppresses the warnings.

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

11 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

Related Questions

Mobile Tap Issue 1 Answer

Hit Detection On Android 1 Answer

How to use Application.OpenUrl("sms") for multiple numbers? 2 Answers

Best FPS cap for mobile builds 1 Answer

Why am I getting an [ERROR: Cannot refresh achievement : VALID_BUT_STALE] response when I call PlayGamesPlatform.Instance.IncrementAchievement? 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