- Home /
 
Placing a Rect and detecting a Touch/Tap
Hi guys, I'm in the progress of creating my first 2D Game.
Since I'm both new to Unity and Android development I have some issues with Touch detection.
When I create a Rect with a height of 50px and a width of 100px, it seems as if the Rect is only registering Touch on the leftmost ~10px or so.
Here is my example code:
 using UnityEngine;
 using System.Collections;
 public class StartUpScript : MonoBehaviour {
 Rect buttonPLAY = new Rect(80, 340, 100, 50);
 
 // Use this for initialization
     void Start () {}
 
     // Update is called once per frame
     void Update () {
         if (Input.touchCount > 0) {
                 if (buttonPLAY.Contains(Input.GetTouch(0).position)) {
                 Application.LoadLevel("GameScreen");
                 }
                 
         }
 }
 
               Also when it works, it feels kinda weird. As if he would fire events more than once or only if you press a certain amount of time. Is there another way to detect a single TAP anywhere on the specified Rect? As I understand it, "Update" is called once per frame. Might this be the problem?
Thanks for the help :)
Can't help you with why the area should be smaller than it is.
For the touch though, it's indeed triggered every frame because you only check if touchCount > 0, which is the case as long as the finger is on screen.
Check
 if (Input.GetTouch(0).phase == TouchPhase.Began){
 ...
 }
 
                 Answer by Clint.Carpenter · Jun 26, 2015 at 02:11 AM
It might be related to the coordinates of the shape and the touch. Try this:
 if (buttonPLAY.Contains(
     Camera.main.ScreenToWorldPoint(
         Input.GetTouch(0).position)) {
 ...
 }
 
              I don't think that is correct. in the docs the example uses Input.mousePosition, which is in pixel coordonates. touch.position is too.
Your answer