Questions about raycastings between UI elements and Scene Objects in 2d multi cameras project

Hi, im making a 2d topdown game using unity
To get seperate lighting and postprocessing effects for my UI and scene, i set multiple cameras.and my UIs are set with ScreenSpace-Camera

and here comes the issues on raycasting
i just found though my UIs appear atop of scene objects, their ray blocking can still be affecting by the scene objects with 2d colliders(which might due to the camera in 2d project always exists behind the scene)
and this would cause my UI interactions not working when its atop of a collider

i asked GPT to write me a script to set cullings on layers for raycasting to prevent scene objects blocking rays,but it wont work.

wonder anyone ever been in such situation (which should be a normal issue i figure) and share some experience on this

btw ,GPTs script goes like this

using UnityEngine;

public class RaycastBlockingToggle2D : MonoBehaviour
{
    // Boolean to track whether raycast blocking is enabled or disabled (initially enabled)
    private bool isBlockingEnabled = true;

    // LayerMask for the objects that should be blocking raycasts (e.g., objects with 2D colliders)
    public LayerMask blockingLayers;

    // Function to toggle raycast blocking for objects with 2D colliders
    public void ToggleRaycastBlocking()
    {
        // Toggle the blocking state
        isBlockingEnabled = !isBlockingEnabled;

        // Log the current state of raycast blocking
        Debug.Log("Raycast blocking is now " + (isBlockingEnabled ? "enabled" : "disabled"));
    }

    // Raycast example to demonstrate the blocking state of the objects with 2D colliders
    void Update()
    {
        // Only perform the raycast if raycast blocking is enabled
        if (Input.GetMouseButtonDown(0) && isBlockingEnabled)
        {
            // Convert mouse position to world coordinates
            Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            // Perform a 2D raycast, only considering the layers specified in the blockingLayers LayerMask
            RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero, Mathf.Infinity, blockingLayers);

            // Check if the raycast hit any 2D collider
            if (hit.collider != null)
            {
                Debug.Log("Raycast hit: " + hit.collider.name);
            }
            else
            {
                Debug.Log("No raycast hit detected.");
            }
        }
    }
}

It’s 2D physics, it knows nothing about depth. The UI (UGUI) has its own “raycasting” set-up dedicated for this.

Know though that because it’s 2D (XY Plane), a raycast of zero-length isn’t how you do it, it’s Physics2D.OverlapPoint.

There is a pseudo-3D raycast for 2D physics that projects the 3D ray into 2D, performs the raycast then sorts the result by Transform Z which might be useful but again, you have to know that fundamentally, physics has no idea about depth and certainly nothing about UI.

Thanks for your reply,now i feel much more clear about the fact that ui ray and physic ray are 2 different concepts.

but from the result i can tell that colliders do block the rays that supposed to be blocked by my UI elements,which means maybe the two ray systems share the same kind of ray?
and even based on this, i dont get why GPT’s way to solve this is not working, it simply let me assign the layers of those objcts with colliders to layermask for physic ray and prevent them from blocking, it quite fit the whole logic ,doesnt it

No, the physics queries see colliders. In-fact, they don’t see colliders, they see the physics shapes the colliders produce. The UI systems in Unity have nothing to do with 2D physics.

god you are right. the two ray systems they should have nothing to do with each others
the reason caused my issues is due to my multiple cameras paratmeters on z axis and culling planes are not alligned
thanks a lot again, been working on this for a whole day

1 Like