Search Unity

Mouse released outside is not possible?

Discussion in 'Scripting' started by RajneeshG, Apr 27, 2015.

  1. RajneeshG

    RajneeshG

    Joined:
    Jan 3, 2014
    Posts:
    15
    I am developing UI for my game. I started with simple Play button. When it is touched, the game will start, but if the user touches the play button and releases touch outside the play button nothing should be happen. How to implement such action. I tried with following code, but else statement in OnMouseUp never executes.

    Is it such that whenever MouseDown is fired, MouseUp on that button will always be fired unless other collider of another game object is under touched?

    Any guidance please?

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0)) {
    2.  
    3.     hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    4.  
    5.     if(hit)    {
    6.      
    7.         if(transform.name == hit.transform.gameObject.name) {
    8.          
    9.             Debug.Log("Button touched");
    10.         }
    11.     }
    12. }else
    13. if(Input.GetMouseButtonUp(0)) {
    14.  
    15.     //hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    16.  
    17.     if(hit)    {
    18.      
    19.         if(transform.name == hit.transform.gameObject.name) {
    20.          
    21.             Debug.Log ("touched up over same button.");
    22.         }else {
    23.             Debug.Log ("touched up not over same button.");
    24.         }
    25.     }
    26. }
     
    Last edited: Apr 29, 2015
  2. akitsu

    akitsu

    Joined:
    Nov 26, 2013
    Posts:
    38
  3. RajneeshG

    RajneeshG

    Joined:
    Jan 3, 2014
    Posts:
    15
  4. RajneeshG

    RajneeshG

    Joined:
    Jan 3, 2014
    Posts:
    15
    Got working with this,

    Code (CSharp):
    1. if(Input.GetMouseButtonUp(0)) {
    2.            
    3.      MyGlobals.hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    4.            
    5.      if(MyGlobals.hit && transform.name == MyGlobals.hit.transform.gameObject.name) {
    6.              Debug.Log ("touched up over same button.");
    7.      } else {
    8.            Debug.Log ("touched up not over same button.");
    9.      }
    10. }
     
    Last edited: May 3, 2015