Hey, I’m trying to make 2D game controls for Android. So I searched around the internet and found some pieces of code. I edited some of them and now I have this. The mouse clicks work perfectly. But the touch input on Android doesn’t. Unity knows it’s an Android device if I build it. The touch is just not working. Did I do something wrong? Is my device too old/broken? I’m using a HTC Desire HD.
#pragma strict
function Update(){
if(Application.platform == RuntimePlatform.Android){
var tapCount = Input.touchCount;
for ( var i = 0 ; i < tapCount ; i++ ) {
var touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began){
checkTouch(touch.position);
}
}
}
else if(Application.platform == RuntimePlatform.WindowsEditor){
if(Input.GetMouseButtonDown(0)) {
checkTouch(Input.mousePosition);
}
}
}
function checkTouch(pos){
var wp : Vector3 = Camera.main.ScreenToWorldPoint(pos);
var touchPos : Vector2 = new Vector2(wp.x, wp.y);
var hit = Physics2D.OverlapPoint(touchPos);
if(hit){
hit.transform.gameObject.SendMessage("RayHit",SendMessageOptions.DontRequireReceiver);
if(Application.platform == RuntimePlatform.WindowsEditor){
Debug.Log(hit.transform.gameObject.name);
}
}
}