Detecting if background is clicked

I’m trying to make a room where you can click and drag to pan the camera, when you click an arrow it will move the camera to another room. The problem is that when I click on the arrow it also launches the function from my Camera Controller that is listening for Input.GetMouseButton(0).

I would like to detect somehow that there were no collider hit and only then should the camera pan. I want this to be general rule, if any collider is clicked don’t run the pan camera script.

This is the current control that moves the camera :

void Update () {

	if(Input.GetMouseButtonDown(0))
		TapPos = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, 0 , 0));
		
	if(Input.GetMouseButton(0))
		MouseDragStart();        
}

void MouseDragStart (){

	if (currentRoom.GetComponent<BgProperties> ().Scrollable) {

		Vector3 _newPos = GetComponent<Camera> ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, 0, 0));
		transform.position = new Vector3 (Mathf.Clamp (transform.position.x + (TapPos.x - _newPos.x), LeftLimit, RightLimit), transform.position.y, transform.position.z);
		TapPos = GetComponent<Camera> ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, 0, 0));	

	}

}

Format code with 101010 button or your questions will be rejected.

There’s a pretty easy way to do this if all the things you can “click on” are confined to a canvas. Just put a big invisible Image element across the whole canvas, behind all other elements, and read its pointer enter/exit events to determine whether you can invoke the camera function with LMB.

Otherwise, my preferred solution would probably be confining all mouse-related logic to a single script, so that you have coherent, explicit control over all mouse-related events. You’d replace any existing mouse-related logic with logic run through this “InputManager” script.

The hit object(s) themselves determine what each input event will do. e.g. if I have ABC script, clicking while I’m under the cursor does XYZ

You could also call logic on scripts on those objects if that’s your preference. e.g. if the mouse ray hit an object with the IClickable interface, invoke its OnClick event.

With this approach, most mouse events should probably be consumed by whatever was clicked (perhaps by return-ing execution after getting handled). So the last event in the control flow would be manipulating the camera: if no other mouse event was handled, you know you didn’t click anything clickable, and are thus clicking “the background”.