- Home /
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?
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
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
The link in your answer doesn't work anymore, could you update it?
Answer by LawyerOfGod · Jan 01, 2014 at 04:17 PM
today, you can use OnMouse functions on Mobile device....
These appears to be correct, see http://docs.unity3d.com/ScriptReference/Input-simulate$$anonymous$$ouseWithTouches.html.
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);
}
}
}
}
I think this needs to be Camera.main.ScreenPointToRay ins$$anonymous$$d of the viewport version of the function.
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.
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.
Your answer
Follow this Question
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