- Home /
Android clicking to touch
Hi, I am trying to convert my game from windows standalone to an android build.
My issue is for clicking an object i use OnMouseUp function but on android this function doesn't work.
is there any function for android that works the same but for touching? i have searched the Script reference but couldn't find any functions for it.
Thanks in advance, Sycohazza.
Answer by whydoidoit · Jun 05, 2012 at 06:05 PM
There is this code that makes OnMouseDown work for multiple fingers on real objects with colliders, you could use this or modify it for OnMouseUp...
// OnTouchDown.cs
// Allows "OnMouseDown()" events to work on the iPhone.
// Attach to the main camera.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class OnTouchDown : MonoBehaviour
{
void Update () {
// Code for OnMouseDown in the iPhone. Unquote to test.
RaycastHit hit = new RaycastHit();
for (int i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
// Construct a ray from the current touch coordinates
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray, out hit)) {
hit.transform.gameObject.SendMessage("OnMouseDown");
}
}
}
}
}
Oh wow i was just using raycast like this about a week ago. Cant believe i didnt think of this, thanks :)
Just stick it in a plugins folder and you can use it from either language
hello ! can we have this code for android please ? It's doesn't work for me :'(
Your answer
![](https://koobas.hobune.stream/wayback/20220613072408im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Place for touch and click command. 1 Answer
Right Mouse Click in Android 2 Answers
How to touch, move and rotate 3D objects in XZ? 1 Answer