I can’t figure out what I’m doing wrong here. I’m just trying to get my selection box to follow the mouse position. Which works fine, until the canvas is scaled up or down based on screen size. Here’s a code snippet where the selection box (visual) is being manipulated.
private void Update()
{
if (isSelecting)
{
currentSelectionPos = Mouse.current.position.ReadValue();
float width = currentSelectionPos.x - selectionStartPos.x;
float height = currentSelectionPos.y - selectionStartPos.y;
selectionBox.rectTransform.sizeDelta = new Vector2(Mathf.Abs(width), Mathf.Abs(height));
selectionBox.rectTransform.anchoredPosition = selectionStartPos + new Vector2(width / 2, height / 2 );
}
}
Here’s a couple of shots of the UI set up.
NOTE - I’ve already tried dividing the anchored position by the Canvas.scaleFactor. Didn’t help. Also tried dividing it by the Canvas.rectTransform.scale.x (or y…it doesn’t matter). Also didn’t help. The box is always offset from where it should be whenever the canvas scale is changed.