- Home /
Detecting touches in a Rect on iOS
I'm new to Unity and I have a noob question here. How do I detect touches in a Rect in iOS? I need a code snippet in C#. For now I've gotten as far as detecting mousePosition in a rect and it works just fine on iPhone but it does not get me close to creating a touch button with a texture, just like as I would want. Here's my code so far:
void Update () {
Rect rect = new Rect(10, 10, 120, 120);
if (rect.Contains(Input.mousePosition))
goLeft();
print("Inside Left");
}
Answer by robertbu · Jan 21, 2013 at 04:50 PM
You are looking for Input.GetTouch(). You need to check to see if any fingers are down, then you can get the position from the Touch data structure. Something like:
if (Input.touchCount > 0)
{
if (rect.Contains(Input.GetTouch().position))
//Do something
}
Note you are getting back screen coordinates.
I'm getting loads of errors when using your approach. Take a look
Well, of course- you need to do the {} after the if! LEARN PROGRA$$anonymous$$$$anonymous$$ING THEN $$anonymous$$A$$anonymous$$E GA$$anonymous$$ES
Your picture did not attach. Post your code, and I'll take a look.
Answer by shiv.k.bhadouria · Feb 15, 2013 at 02:46 PM
Try This :
Replace if (rect.Contains(Input.mousePosition))
with
if (rect.Contains(Input.mousePosition(0)))
Your answer
Follow this Question
Related Questions
GUI.Button lags on iPhone 0 Answers
How to set up virtual D-Pad for iOS? 1 Answer
TouchPhase.Stationary lags on iOS 0 Answers
ios pinch/tap/swipe/pan/(...) gesture 4 Answers
How can I implement iOS TouchScreenKeyboard 'Next' and 'Go' keys? 0 Answers