- Home /
 
Raycast Touch Android not working JS
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);
         }
     }
 }
 
              Answer by pacific00 · Jul 04, 2014 at 12:31 PM
input.getmouseButton would work on all platforms.. itd work on android..
Answer by meat5000 · Jul 04, 2014 at 01:13 PM
You are missing your z Depth parameter on your ScreenToWorldPoint.
You are passing your checkTouch function a Vector2 only.
ScreenToWorldPoint requires a Vector3. Read the link.
touch.position is a Vector2. Take it or leave it :P
Debug.Log(touch.position);
to see if anything is going in before you call your function.
Your answer
 
             Follow this Question
Related Questions
How to properly implement snake style controls for android? 0 Answers
I Need Help With Mobile Touch Movement. 0 Answers
Android touch 3d Object event 1 Answer
Touch to move 3 Answers
Best way to do touch in with Unity2D? 0 Answers