How to stop the movement when touching UI buttons on mobile

Hello! I am using a script to rotate the camera around with touches.

I want to block the movement when I touch a button, slider or any other UI element.

I am using this code:

void FixedUpdate()
    {
        if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId))
        {
            if (Input.touchCount > 0)
            {
                touch = Input.GetTouch(0);

                if (touch.phase == TouchPhase.Moved)
                {
                    rotationY = Mathf.Clamp(rotationY, -minY, minZ);

                    transform.rotation = originalRotation * Quaternion.Euler(0, rotationY, 0);
                }
            }
        }
    }

What is wrong?
The camera can no longer be moved even if I don’t touch any UI element.

If I remove if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId)) the camera can be moved again.

How to block the movement just if I touch the UI elements?
Thank you!

I see you’re setting a value for touch after you try to do all your checks. Just make sure that value is correct. I would even say, double check Unity’s example and try to mimic it a bit more closely in the order they do their checks. Make sure that it’s working the way they have it also.

But my first guess is the value you’re passing in for touch.fingerId may not be what you think it is. The other possible issue is you are hitting a UI element without realizing it, so you’ll want to double check that.

Just some things to look at.