- Home /
Scrollrect snap laggy on mobile
So I've created my own Scrollrect that snaps to elements. This works great in the editor but when building to mobile the elements of the scrollrect start jittering and shaking in a weird fashion. What could be the problem? Could it be something with lerp? how do I get it scroll and snap smoothly?
Here are the main parts of the code, the elements are in a gridlayout and I'm using an unrestricted scrollrect for the scrolling:
void Update()
{
if (!shopController.draggingCategories)
{
GetClosestImg(shopController.centerCategories.transform.position.x);
shopController.ActivateCategory(minImgNum);
LerpToImg(minImgNum * -imageDistance);
}
}
private void GetClosestImg(float center)
{
for (int i = 0; i < UIList.Count; i++)
{
distance[i] = Mathf.Abs(center - UIList[i].content.transform.position.x);
}
float minDistance = Mathf.Min(distance);
for (int a = 0; a < UIList.Count; a++)
{
if (Mathf.Approximately(minDistance, distance[a]))
{
minImgNum = a;
}
}
}
public void LerpToImg(int position)
{
float newX = Mathf.Lerp(ScrollPanel.anchoredPosition.x, position, 10 * Time.deltaTime);
Vector2 newPosition = new Vector2(newX, ScrollPanel.anchoredPosition.y);
ScrollPanel.anchoredPosition = newPosition;
}
Comment
Another thing is that this only happens on Android and not ios, tested on Huawei 9 mate pro and iPhone 6s so I doubt it's a performance issue.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162028im_/https://answers.unity.com/themes/thub/images/avi.jpg)