- Home /
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.EventSystems
but 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.y,
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(
transform.position.x,
Mathf.Lerp(ZoomMin, ZoomMax, ZoomPercentage),
transform.position.z);
transform.rotation = Quaternion.Euler(
Mathf.Lerp(TiltZoomMin, TiltZoomMax, ZoomPercentage),
0.0f,
0.0f);
}
}
}
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.
Answer by donutLaserDev · Jul 02, 2018 at 02:37 PM
https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html
This will tell you whether the cursor is above the UI element.
Is that really the recommended way of doing it? Cause that feels kinda dirty to me. If this is indeed the proper way of handling this, I will accept your answer.