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:

  1. Determine the pressed-over UI object is actually this.gameObject and not one which is overlapping in front.
  2. Determine the position of the click, in the same coordinate space as the image being clicked.
  3. 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)


1 Answer 1


Please see the following.

  1. In order to make sure that the user clicks on the required image, you can adjust the sort order of the canvas component containing the image.
  2. For finding the coordinate of the click, you can use the RectTransformUtility.ScreenPointToLocalPointInRectangle() method.
  • \$\begingroup\$ Thx I will check when back home. Have +1 meanwhile \$\endgroup\$ May 10, 2016 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .