How to detect the proper World space UI element’s position using GraphicRayCaster?

Good afternoon. I am trying to interact with UI elements within a worldspace canvas due to Unity 2022 LTS not supporting interaction via the New Input System when using a locked cursor. The issue I am having is that I am able to detect the UI elements but it seems like it is detected UI locations are not at the correct location. It seems like it is scaled down from the X axis or something. There is no detection issues on the Y axis. It only seems like the x positions are scaled down or offset.

For example the button shown below is 160×30 in size centered in the middle of the canvas. The red outline is where the UI element is detected by the Graphic Raycaster. If reduce the button element size to 80px instead of 160px the detected location is also halved as well despite the canvas size not changing.

The same is true for the other elements which I highlighted where they are being detected at despite their locations not changing within the canvas itself.

detected locations

Some things to note:
-Canvas is parented to a gameobject which is scaled down to .00025 size in world units to match it to the size it should be within worldspace.

-Canvas size is 1920×1080.

-Canvas has a box collider on it to detect a physics-based ray for when the player is looking at it. It is set to also (1920x, 1080, .001f)xyz. There’s no problem with detecting the canvas and it gives the correct canvas coordinates that are being looked at by the player as the cursor is locked at the center of the screen and an aiming reticle is used to show where the player is looking.

-The old input system is still enabled, but is only referenced in code for backwards compatibility as some assets still need it that I use. Otherwise, everything else that I do and use make use of the New Input System.

What could cause this behavior? Am I not calculating the canvas coordinates correctly or am I missing something that would cause this?

I’ve added the code I am using to obtain the canvas that is being looked at and the location. The location info is then passed into the OnInput() function which handles and processes input via the results gathered using the Graphic Raycaster on the canvas.

void Update()
    {
        // Cast a ray from the mouse position.
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        
        // Temporary hit variable to store hit information.
        RaycastHit hit;
        
        // Raycast with the calculated information.
        if(Physics.Raycast(ray, out hit, 1.5f, worldSpaceMask))
        {
            if (graphicRaycaster == null)
            {
                graphicRaycaster = hit.transform.GetComponent<GraphicRaycaster>();
                canvasRect = hit.transform.GetComponent<RectTransform>();
            }
            
            // Translate the hit location into canvas space coords.
            Vector2 localPosition = hit.transform.InverseTransformPoint(hit.point);
            Vector2 CanvasPosition = new Vector2(
                (localPosition.x + (canvasRect.rect.width * canvasRect.pivot.x)), 
                (localPosition.y + (canvasRect.rect.height * canvasRect.pivot.y)));
            
            // Raycast using Graphic Raycast to determine hits & handle events. 
            OnInput(CanvasPosition);
        }
        else if (graphicRaycaster != null)
        {
            graphicRaycaster = null;
            canvasRect = null;
        }
    }

I’ve tried changing the size of the UI elements to test to see if the detected locations changed despite the canvas size not changing. The detected locations where only changed when the elements size was changed.

New contributor

user171859 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật