Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help to make unclickable area

Discussion in 'Scripting' started by NORWAYY, Mar 7, 2021.

  1. NORWAYY

    NORWAYY

    Joined:
    Nov 10, 2020
    Posts:
    17
    This is my script
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         Vector3 mousePos = Input.mousePosition;
    4.  
    5.         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mousePos);
    6.         mouseWorldPosition.z = -2;
    7.         if (Input.GetMouseButtonDown(0))
    8.         {
    9.          
    10.      
    11.          
    12.         }
    13.      
    14.     }
    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?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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.
     
  3. NORWAYY

    NORWAYY

    Joined:
    Nov 10, 2020
    Posts:
    17
    I think I was not clear what I wanted, I want to say a enemy area. If I click there the it wont work:
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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.