Click screen to fire or game object

Hey peeps,

I have a 2d top down game where I want the user to be able to press anywhere on the screen to fire projectiles from the player. So I don’t really need any co-ordinates for the click. I just don’t know how to achieve this. My biggest problem is I also have a game object with a pause button sprite attached to it. If the user clicks that I dont want the player to fire but rather run the pause game method.

How would I go about setting this up?

  1. Im not sure how to do a generic screen click for the iPhone using unity.
  2. Is it simply attaching OnMouseDown code to a game object to detect if it was pressed with the iphone?
  3. I’m not sure how I would override the screen click when I hit the pause button game object or any other game object I may attach click code to.

I thought it would be easy to find these answers somewhere but I did a little searching and cant find much. All the stuff I managed to find is OnGUI stuff and I’m trying to avoid using GUI.

Any thoughts?

OnMouseDown doesn’t work on the iPhone but you can detect the click quite easily from the Update function. The Input class lets you get touches on the screen quite easily. It is also quite straightforward to ignore the pause button:-

var pauseButtonRect: Rect;

function Update() {
  //  If there is a touch...
  if (Input.touchCount != 0) {
    //  If it isn't inside the pause button...
    var touch = Input.GetTouch(0);

    if (pauseButtonRect.Contains(touch.position)) {
      //  Pause.
    } else {
      // Fire!
    }
  }
}

That is perfect! I really appreciate the reply. Thanks heaps.