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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Layer Mask Help

Discussion in 'Scripting' started by will_brett, Aug 12, 2014.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Hey all,

    I've created a simple script to move my player to a location of a click/touch. Im now trying to stop the character from moving if an NGUI object is clicked. Im trying to do this with layermasks but no such luck thus far.

    Here is my code:

    Code (CSharp):
    1. //function call when event is fired
    2.     void OnMultiTap(Tap tap)
    3.     {
    4.         if(InputOnNGUIObject(tap.pos)) return;
    5.  
    6.         //Your custom code;
    7.         Ray ray = Camera.main.ScreenPointToRay(tap.pos);
    8.         RaycastHit hit;
    9.  
    10.         if (_terrain.collider.Raycast (ray, out hit, Mathf.Infinity))
    11.         {
    12.             _newLocation.position = hit.point;
    13.         }
    14.     }
    15.  
    16.     bool InputOnNGUIObject(Vector2 inputPos)
    17.     {
    18.         Ray ray = _cam.ScreenPointToRay(inputPos);
    19.         RaycastHit hit;
    20.         //NGUILayer is the layer number set for ngui object
    21.         LayerMask mask = 8;
    22.  
    23.         if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask.value))
    24.         {
    25.             Debug.Log("Mask hit");
    26.             //the raycast hit something, means the input lands on ngui object
    27.             return true;
    28.         }
    29.         return false;
    30.     }
    The gui Layer is layer 8. The On Multitap stuff is the Input.Touches plugin from the asset store and this stuff is all working fine, its just the character still moves to the click/ touch location when an NGUI object is clicked. Anyone have any ideas as to why?

    Thank you
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Thanks hpjohn. Unfortunately this hasn't fixed the problem. I think its something to do with the camera and UICamera both doing a raycast which may be the issue.
     
  4. trololo

    trololo

    Joined:
    Dec 13, 2012
    Posts:
    17
    Use something like:
    if(UICamera.hoveredObject != null && UICamera.hoveredObject.layer == LayerMask.NameToLayer("NGUILayer"))
    {
    // Don't move
    }
     
  5. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    trololl... That works perfectly. Thank you so much. Will have to dissect that I'd figure out exactly what it does but that was a huge help.