- Home /
Detect if mouse if hovering over UI element to change behavior of mouse wheel input
Consider the following code existing in the update function of a script attached to camera:
float zoom = Input.GetAxis("Mouse ScrollWheel");
if (zoom != 0) {
translation.y -= zoom * zoomSpeed;
}
Just your basic run of the mill input handling for mouse wheel input; which I use for zooming.
This breaks down though when I start adding UI menus. I have a menu (Panel) with some scrollable content. When I hover over the UI, I want to disable the mouse wheel zoom on the camera.
I tried doing this:
if (!EventSystem.current.IsPointerOverGameObject()) {
float zoom = Input.GetAxis("Mouse ScrollWheel");
if (zoom != 0) {
translation.y -= zoom * zoomSpeed;
}
}
But this disables the mousewheel if I am hovering over ANY gameobject. How can I test to see if I am hovering over a UI related gameobject?
Answer by rageingnonsense · Apr 15, 2015 at 07:38 PM
I ended up resorting to adding two event triggers (PointerEnter and PointerExit) for each menu I have where I want to disable mouse zoom for the camera. I then added two methods to my camera look script to set a flag which define if we should respect mouse wheel input. The even triggers call these as needed.
It works, but was hoping for something a bit cleaner and centralized to the camera script.
This also worked for me, but hopefully there is a better way of doing this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613193220im_/https://answers.unity.com/themes/thub/images/avi.jpg)