- Home /
How to make a snapping scroll menu C#
Hello. i am trying to create a ScrollMenu that snaps towards its items. i've tried to follow this YouTube tutorial https://www.youtube.com/watch?v=9B7ahj1kaYs to make it happen. the problem is that i want to make my menu vertical. Sadly, it doesn't snap to the items. in fact, it automatically scrolls down to the last item and refuses to scroll. I tried playing around with the code to find out what's wrong and i assume that the problem is on line 16. What is wrong with the code? any help is very much appreciated.
P.S. : i realize that there is an empty GameObject to compare the closest to it. and its not a child of the scroll panel.
private void Start()
{
int charLength = characters.Length;
distance = new float[charLength];
distanceBetweenCharacters = 200;
}
private void Update()
{
for (int i = 0; i < characters.Length; i++)
{
distance[i] = center.transform.position.y - characters[i].transform.position.y;
}
float minDistance = Mathf.Min(distance);
Debug.Log(distanceBetweenCharacters);
for (int a = 0; a < characters.Length; a++)
{
if (minDistance == distance[a])
{
minCharNum = a;
}
}
if (!isDragging)
{
LerpToChar(minCharNum * distanceBetweenCharacters);
}
}
void LerpToChar(float position)
{
float newY = Mathf.Lerp(panal.anchoredPosition.y, position, Time.deltaTime * 5f);
Vector2 newPosition = new Vector2(-386, newY);
panal.anchoredPosition = newPosition;
}
public void StartDrag()
{
isDragging = true;
}
public void EndDrag()
{
isDragging = false;
}
Your answer
Follow this Question
Related Questions
How to find the minimum number in a float array C# 1 Answer
How can i make Scrolling Menu? 2 Answers
Creating slider menu for selecting an option using scroll view unity? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers