Help to make unclickable area

This is my script

private void Update()
    {
        Vector3 mousePos = Input.mousePosition;

        Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mousePos);
        mouseWorldPosition.z = -2;
        if (Input.GetMouseButtonDown(0))
        {
         
      
         
        }
     
    }

I want to check if the Input on the mouse button and if I am not clicking on a not clickable area. But I need to make a area as well, any suggestions on what I can do?

There are many ways to accept click/tap input, and each way requires a different way to block.

If it is just UI (such as a button or slider or what-have-you underneath a Canvas), you can:

  • disable it or otherwise set it inactive
  • set it not interactable (this may make it dim out, depending on configuration)
  • cover it with visible (or invisible) blocker(s) (Graphic object marked RaycastTarget)

If it is a function like Input.GetMouseButtonDown(0), that will ALWAYS register, no matter what, so now you need to alternately ask, where is the mouse and is it blocked or should I pay attention to this click.

Same goes for Input.touches, the array of touches on mobile. They will arrive; it’s up to you to decide not to respond.

As always, it’s helpful to break the steps down:

The underlying input system produces low-information “I clicked at this coordinate” data.

At some point your code decides “Hey, that click means X to my program!”

And then at some point you act upon that “X” and actually go and do the X.

Blocking can take place anywhere along the line, whatever is logically most convenient for you.

I think I was not clear what I wanted, I want to say a enemy area. If I click there the it wont work:

Define the area you cannot click on, perhaps with a collider. If it’s a collider, raycast at it and decide “Nope, can’t click there.”

Alternately define the area that you CAN click. Depending on if you have more clickable or more unclickable, one approach may be better.