I'm working on an inventory grid system in Unity and having trouble with coordinate conversion in my GetSlotIndexFromPosition method. When I click on the grid slots, I'm getting negative grid coordinates like (-4, 1), (-3, 0) instead of the expected positive values like (0, 0), (1, 0), etc. The issue seems to be related to how I'm converting the local coordinates from RectTransformUtility.ScreenPointToLocalPointInRectangle() to grid coordinates. I struggle with math's in general, so I'm unsure what I am doing wrong here.
{
///coords -= (Vector2)transform.position;
Vector2Int gridCoords = new Vector2Int(
Mathf.FloorToInt(coords.x / (slotCellSize + slotPadding)),
Mathf.FloorToInt(coords.y / (slotCellSize + slotPadding))
);
int index = gridCoords.x + gridCoords.y * gridSize.x;
print(gridCoords);
return 0;
}
public void OnPointerClick(PointerEventData eventData)
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(
transform as RectTransform,
eventData.position,
eventData.pressEventCamera,
out Vector2 localPoint
);
GetSlotIndexFromPosition(localPoint);
}```