- Home /
 
 
               Question by 
               Paramotion · Jul 04, 2017 at 03:09 PM · 
                androidinputtouchtouchscreen  
              
 
              Touch Input madness
I'm trying to detect some objects on the screen by touching them, and recognizing them by their tag. I've been playing around with the Input.Touch, but, something is wrong in my code.
 public void checkHit (Collider col)
     {
         Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
         RaycastHit hit;
         if (Input.touchCount >= 1)
         {
             if (Physics.Raycast (ray, out hit, 200))
             {
                 switch (Input.GetTouch (0).phase)
                 {
                     case TouchPhase.Began:
                     
                     if (gameObject.CompareTag ("smallI")) 
                     {
                         Debug.Log ("Touched smallI");    
                     } 
                     else if (gameObject.CompareTag ("smallC")) 
                     {
                         Debug.Log ("Touched smallC");
                     }
                     else if (gameObject.CompareTag ("smallB")) 
                     {
                         Debug.Log ("Touched smallB");    
                     }
                     break;
                 }
             }
         }
     }
 
               If someone has an idea of what should I change, or where to look to solve this, it'd be great!
Thanks in advance!
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Paramotion · Jul 05, 2017 at 08:29 AM
Nevermind, the problem was I forgot to pass the touchCount trough the Update (). Fixed.
Answer by efeguclu · Jul 04, 2017 at 03:20 PM
Check this :
 public void checkHit (Collider col)
      {
          Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
          RaycastHit hit;
          if (Input.touchCount >= 1)
          {
              if (Physics.Raycast (ray, out hit, 200,col.gameObject.layer;))
              {
                  switch (Input.GetTouch (0).phase)
                  {
                      case TouchPhase.Began:
                      
                      if (col.gameObject.CompareTag ("smallI")) 
                      {
                          Debug.Log ("Touched smallI");    
                      } 
                      else if (col.gameObject.CompareTag ("smallC")) 
                      {
                          Debug.Log ("Touched smallC");
                      }
                      else if (col.gameObject.CompareTag ("smallB")) 
                      {
                          Debug.Log ("Touched smallB");    
                      }
                      break;
                  }
              }
          }
      }
 
              Your answer