Search Unity

Prevent a click/touch from going through GUI buttons.

Discussion in 'Immediate Mode GUI (IMGUI)' started by Akshaypure, Sep 29, 2014.

  1. Akshaypure

    Akshaypure

    Joined:
    Sep 8, 2014
    Posts:
    4
    In my game I want player to point gun where user clicks.
    On left user can select different weapons. Everything is working except when I click on gun to select it, player points at that button. How can I avoid that.
    if(Input.GetMouseButtonDown)
    {
    // rotate gun
    }

    Please help
     
  2. twick

    twick

    Joined:
    Nov 16, 2011
    Posts:
    31
    I would also like to know how to prevent click throughs. Prior to Unity 4.6 r19 you could check Event manager to see if the mouse was over a GUI element like this :

    Code (CSharp):
    1. if (!EventSystemManager.currentSystem.IsPointerOverEventSystemObject())
    Unfortunately in r19, EventManager was removed.
     
    Akshaypure likes this.
  3. twick

    twick

    Joined:
    Nov 16, 2011
    Posts:
    31
    Scratch my last post;

    Use this
    Code (CSharp):
    1. EventSystem.current.IsPointerOverGameObject()
    It returns false if your mouse pointer is over the GUI
     
    rraallvv likes this.
  4. Akshaypure

    Akshaypure

    Joined:
    Sep 8, 2014
    Posts:
    4
    EventSystem class not found in unity. And also it will prevent selecting all the game objects.

    here I want to exclude all buttons:
    if(Input.GetButton ("Fire1"))
    {
    Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
    difference.Normalize (); //normalising the vector. Meaning all the sum of the vector will be equal to 1.

    float rotZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg; //find the angle in degrees.
    transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotationOffset);
    }
     
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    You still need to import UnityEngine.EventSystems to use it.
     
    Akshaypure likes this.
  6. Akshaypure

    Akshaypure

    Joined:
    Sep 8, 2014
    Posts:
    4
    :( Not working..is there anything in new unity 4.6 UI...
     
  7. Rati

    Rati

    Joined:
    Jun 26, 2011
    Posts:
    25
    I got "true" when i'm on a unity 4.6 ui panel