Keeping values beyond one decimal place in a RectTransform pivot (Vector2)

Hi,

I was wondering if there is a way to keep precise values while changing the pivot of a RectTransform. What I am trying to accomplish is scaling the RectTransform based on the mouse position. I found this thread , which seems like a viable solution (see SetPivot method). I’ve implemented this hack job, similar the thread in my own OnScroll method from the IScrollHandler interface that is attached to my Class, which is here:

public void OnScroll(PointerEventData eventData)
{

    if (eventData.scrollDelta.y > 0 && mapRectTransform.localScale.x < maxZoomLevel)
    {
        RectTransformUtility.ScreenPointToLocalPointInRectangle(mapRectTransform, eventData.position, uiCam, out Vector2 localPoint);
        Rect rect = mapRectTransform.rect;

        float pX = (localPoint.x - rect.x) / (rect.xMax - rect.x);
        float pY = (localPoint.y - rect.y) / (rect.yMax - rect.y);

        Debug.Log($"{pX}, {pY}");   // will get digits up to the  (n) * 10 ^ -7 digit

        // But here is the problem. When you set the pivot, everything beyond the (n) * 10 ^ -1 digit is cut off.
        mapRectTransform.pivot = new Vector2(pX, pY);

        // Scrolling up. Zoom in;
        ZoomIn(zoomRate);
    }
    if (eventData.scrollDelta.y < 0)
    {
        // Essentially the same
        ZoomOut(zoomRate);       
    }
}

However, as noted in the OnScroll method, when pX and pY is placed into a new Vector2, everything beyond the 10th decimal place is cut off (i.e, the new pivot is 0.4 rather than 0.4323712). I need the precise values because my the mapRectTransform is pretty large in size. In any case, my question is, is there a way to keep the values beyond the first decimal place while setting the RectTransform pivot?

Sorry, this isn’t the right forum for questions on RectTransform :slight_smile: This forum is UI Toolkit only and your question is for UGUI.
You should try to post here instead: Unity Engine - Unity Discussions

So I realized that the float does keep the decimal values but Debug.Log() truncates the numbers when writing to the console.

1 Like