IsPointerOverGameObject detects UI elements over worldspace

Hello, Ive been trying multiple scripts endlessly these past 3 days and this is the script i found best so far. The problem is that it detects UI elements even in the world space. The game I’m currently doing is an fps and I use a UI element in worldspace for a clickable bed. Would be really helpful if you could help me out!

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;

public class ScreenSwipeTest : MonoBehaviour
{
    public float mouseSensitivity = 15f;
    public Transform playerBody;
    public Transform playerCamera;

    float xRotation;

    void Update()
    {
        float mouseX = 0;
        float mouseY = 0;

        // Check if there are any touches
        if (Touchscreen.current.touches.Count > 0)
        {
            foreach (var touch in Touchscreen.current.touches)
            {
                // Check if the touch is over a UI element
                if (EventSystem.current.IsPointerOverGameObject(touch.touchId.ReadValue()))
                    continue;

                // Read touch delta position
                Vector2 touchDeltaPosition = touch.delta.ReadValue();
                mouseX += touchDeltaPosition.x;
                mouseY += touchDeltaPosition.y;
            }
        }

        mouseX *= mouseSensitivity * Time.deltaTime;
        mouseY *= mouseSensitivity * Time.deltaTime;

        // Rotate the player body left and right
        playerBody.Rotate(Vector3.up * mouseX);

        // Rotate the camera up and down
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -80, 80);

        // Apply rotation to the camera
        playerCamera.localRotation = Quaternion.Euler(xRotation, 0, 0);
    }
}

I found this on the internet (separate scripts) then merged them