ScrollRect question

Hello!
I have a Scroll Rect setup with a bunch of objects inside (let’s say 5 for now). Let’s also say that the scrollrect is horizontal.

I don’t want to explain everything that I am trying to do because i think it would just get more confusing, but basicly I am setting, in code, the position of scrollrect.content.position to focus on specific items. In some cases, the position of my formula will return a value that exceeds the 0-1 threshold of scrollrect.horizontalnormalizedPosition which causes weird behaviour.

So what I would like to do is clamp the value I am getting (which is in world units, or RectTransform.position) to never go under 0 or above 1 in the scrollrect, but I have no idea how scrollrect.horizontalnormalizedPosition is actually calculated. How can I convert this?

This is the function that I used based which returns the position which I am scrolling the scrollrect.content.position to.

private Vector3 GetPos(int i_Index)
{
Vector3 maskCenterPos = m_MaskTransform.position + (Vector3)m_MaskTransform.rect.center;

Vector3 itemCenterPos = m_Panels[i_Index].position;
Vector3 difference = maskCenterPos - itemCenterPos;
difference.z = 0;
Vector3 newPos = m_ScrollRect.content.position + difference;
if (m_ScrollRect.vertical)
{
newPos.x = m_ScrollRect.content.position.x;
}
if (m_ScrollRect.horizontal)
{
newPos.y = m_ScrollRect.content.position.y;
}
return newPos;
}

Sorry, I don’t know where the hell to click for code tags.

Using code tags properly - Unity Engine - Unity Discussions for code tags

With horizontalNormalizedPosition, I believe the value is 0 is the far left, 1 is the far right. (with verticalNormalizedPosition, bottom is 0 and top is 1).

What that means is when you scroll the content window all the way to the left, you should get a horizontalnormalizedposition of 0 and to the right will give 1 (you can print out these values if you need to to varify).

If you simply need to keep it within 0-1, then you can check if the value is less than 0, set it to 0 or greater than 1, set it to 1 and you should be fine.

For complicated reasons, I tried to do that but it won’t work with the scrolllist’s elasticity setting which I want to keep.
What I need to do is this :

private Vector3 GetPos(int i_Index)
{
Vector3 maskCenterPos = m_MaskTransform.position + (Vector3)m_MaskTransform.rect.center;

Vector3 itemCenterPos = m_Panels[i_Index].position;
Vector3 difference = maskCenterPos - itemCenterPos;
difference.z = 0;
Vector3 newPos = m_ScrollRect.content.position + difference;
newPos = CheckOutOfBounds(newPos);
if (m_ScrollRect.vertical)
{
newPos.x = m_ScrollRect.content.position.x;
}
if (m_ScrollRect.horizontal)
{
newPos.y = m_ScrollRect.content.position.y;
}
return newPos;
}

Basicly it is the CheckOutOfBounds function (line 10) that I need to write that needs to check if the .position value would be lower than 0 or greater than 1 once the scrollrect is set, but I have no idea for the math behind it.