LooMeenin
LooMeenin

Reputation: 818

How to activate a button only when the player releases from within button boundary?

I want my button to work only when a player releases from within the button boundary. If the player slides his/her finger over and exits the boundary of the button, I don't want it to do anything. This is for a touch based interface.

What can I add to the following code to make it work:

void OnMouseUp(){

            Application.LoadLevel ("MoonWalker");

    }

Upvotes: 0

Views: 40

Answers (1)

Jay Kazama
Jay Kazama

Reputation: 3277

First of all, for touch based interface you should use TouchPhase instead. (I've posted the link in my comment)

To make the button work only when touch released within its boundary, you'll need Raycast to detect if the position of touch release is within the button or not. If it is then do something, if it is not then do nothing.

void Update(){
   foreach(Touch touch in Input.touches) {
      if(touch.phase == TouchPhase.Ended) {
         Ray ray = Camera.main.ScreenPointToRay(touch.Position);
         if (Physics.Raycast(ray, out hit)) {
            if(hit.GameObject.name == "Button")
               //do something
         }
      }
   }
}

Upvotes: 1

Related Questions