How to detect a touch inside a GUI
Hello,
I'm new with Unity and i try to detect if the player's finger is in a certain part of the screen that will works a little bit like a virtual joystick.
I wrote this :
Touch touch = Input.GetTouch(0);
if((touch.position.x >= wheelCanonImage.rectTransform.position.x
&& touch.position.x <= (wheelCanonImage.rectTransform.position.x + wheelCanonImage.rectTransform.sizeDelta.x))
&& touch.position.y >= wheelCanonImage.rectTransform.position.y
&& touch.position.y <= (wheelCanonImage.rectTransform.position.y + wheelCanonImage.rectTransform.sizeDelta.y)){
touchText.text = "INSIDE !";
}
else{
touchText.text = "Out";
}
Where wheelCanonImage is a Image type (the image in the canvas for the UI). This code works, but i'm sure there is a way less less awful than that in Unity.
Thanks already for the help !
Comment
Your answer
Follow this Question
Related Questions
Display list as UI or GUI 0 Answers
UI penetration through objects. 1 Answer
How to detect with colliders while drawing line by line renderer? 1 Answer
Adding points for every gameObject on the scene instead of adding points for the destroyed one 0 Answers
How to make a restart button pop up after character has died? 2 Answers