In this example, there is a left and right button that moves the character in that direction. Both buttons have a box collider.
leftBox = GameObject.Find ("leftButton").GetComponent<BoxCollider2D> (); rightBox = GameObject.Find ("rightButton").GetComponent<BoxCollider2D> (); void Update () { foreach (var touch in Input.touches) { if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) { Vector2 wp = Camera.main.ScreenToWorldPoint (touch.position); Vector2 touchPos = new Vector2 (wp.x, wp.y); if (leftBox == Physics2D.OverlapPoint (touchPos)) { //do stuff } if (rightBox == Physics2D.OverlapPoint (touchPos)) { //do stuff } } } }
Basically, we check each touch, convert it to world space and then check if the touch point is over the box collider area.
Leave a Reply