1

I created a script where by clicking on the smartphone display, an object was activated. I used Input.GetMouseButtonDown(0) to check if a tap was made on the display. The script works fine, but I have a problem: if I insert a button in the scene, clicking on the button I receive both the click of the button and that of the Input.GetMouseButtonDown function. How could I "divide" the two touches?

// Example
void Update()
{
    if (Input.GetMouseButtonDown(0))
    myObject.SetActive(true); 
}
            
public void PauseButton()
{
    // Open Pause Panel
            
}

I was thinking of deleting "Input.GetMouseButtonDown (0)" and using an invisible button and resizing it on the portion of the display I needed.

3
  • Why you need both, button and a touch? What are you trying to achieve? Commented Sep 4, 2020 at 17:45
  • @rootpanthera I I need to control the touches on the display to activate the item. But in the same scene as the object to be activated, I would like to insert a button to open a pause menu. In this way, however, the touches on the button are equivalent to the one that activates "MyObject". Commented Sep 4, 2020 at 17:49
  • 1
    Input.GetMouseButtonDown is called upon every touch. If possible, you can add an invisible button like you suggested. Another way would be to check the position of the touch with touch.position. Commented Sep 4, 2020 at 19:41

1 Answer 1

1

Add Tag to your UI button like: "UI"

Below function tells whether a click was Over a UI object tagged with a UI tag, so you can check in your screen tap function if you should fire your event or not

public static class InputHandler
{
    public const string CompareTagForUIElements = "UI";

    public static bool IsPointerOverUIObject(int touchId)
    {
        var eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        // I'm using touch since you are talking about smartphones, 
        // if you still need click use Input.mousePosition
        eventDataCurrentPosition.position = new Vector2(GetTouch(touchId).position.x, GetTouch(touchId).position.y);
        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
        for (int i = 0; i < results.Count; i++)
        {
            if (results[i].gameObject.CompareTag(CompareTagForUIElements))
                return true;
        }
        return false;
    }
}

So rather than

public void ITappedOnScreen()
{
    if(Input.GetMouseButtonDown(0))
        Debug.Log("Screen tapped");
}

use

public void ITappedOnScreen()
{
    if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Begin &&
        !InputHandler.IsPointerOverUIObject(0))
        Debug.Log("Screen tapped");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.