Blocking Input.ButtonDown and Input.GetAxis when over UI,Block Input.GetButton when over an UI Element

Hi there,

I tried searching for a concise answer to my problem, but so far I’ve had no luck. I get that the answer lies somewhere in UnityEngine.EventSystemsbut so far I’ve not been successful in using this correctly.

The problem is as follows: I got a Camera with a script attached to it:

public class PlayerCameraController : MonoBehaviour
	public float LookSpeed = 0.4f;
	public float ZoomSpeed = 1.0f;
	public float TiltZoomMin = 70.0f;
	public float TiltZoomMax = 45.0f;
	public float ZoomMin = 20.0f;
	public float ZoomMax = 10.0f;

	private float ZoomPercentage = 0.0f;

	private void Update()
		if (Input.GetButton("MoveCamera"))
			var mouseX = Input.GetAxis("MouseX");
			var mouseY = Input.GetAxis("MouseY");

			transform.position = new Vector3(
				transform.position.x + mouseX * LookSpeed,
				transform.position.z + mouseY * LookSpeed);

		var scrollValue = Input.GetAxis("MouseScroll");

		if (scrollValue != 0.0f)
			ZoomPercentage = Mathf.Clamp(ZoomPercentage + scrollValue * ZoomSpeed, 0.0f, 1.0f);

			transform.position = new Vector3(
				Mathf.Lerp(ZoomMin, ZoomMax, ZoomPercentage),

			transform.rotation = Quaternion.Euler(
				Mathf.Lerp(TiltZoomMin, TiltZoomMax, ZoomPercentage),

This script works great, the problem is that there’s no way for me to block the camera from being zoomed while my cursor is over the UI. I cannot exactly see how to do this with UnityEngine.EventSystems either, as there’s no reference to the scroll wheel? And it seems to ignore IPointerClickDown too.

Any help here would be greatly appreciated.

This will tell you whether the cursor is above the UI element.