I have a Canvas set as Screen Overlay, and an Image there. I want my image become an 8-direction custom pad, so I wanted to make a component script for the image.
In such behavior I would like to detect, on each frame (Update()
) whether the mouse (or touch 0) is being pressed over the current object, say:
public class MyBehavior : MonoBehavior {
void Update() {
if (Application.isPlaying && Input.GetMouseButton(0)) {
// TODO Verify the mouse is pressing over this.gameObject
} else if (Application.isMobilePlatform && Input.touchCount > 0) {
Touch touch = Input.GetTouch(0);
if (touch.phase != TouchPhase.Canceled && touch.phase != TouchPhase.Ended) {
// TODO Verify the touch is pressing over this.gameObject
}
}
}
}
What I need is:
- Determine the pressed-over UI object is actually
this.gameObject
and not one which is overlapping in front. - Determine the position of the click, in the same coordinate space as the image being clicked.
- My goal is to calculate the appropriate direction of the click (angle) wrt the pivot of the image. I will process that direction value. I will also calculate the appropriate distance to the pivot, and process it.
How can I achieve that goal?
(Notes: The test I want to do for standalones and mouse are actually for debug purposes)