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.
user171859 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.