Question by
Muhamadhariyanto · Apr 23, 2020 at 03:18 PM ·
scripting problemscenescripting beginner
Why when i slide my content its always stuck
here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class swipe_control : MonoBehaviour
{
public GameObject scrollbar;
float scroll_pos = 0;
float[] pos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
pos = new float[transform.childCount];
float distance = 1f / (pos.Length - 1f);
for (int i = 0; i < pos.Length; i++){
pos[i] = distance * i;
}
if (Input.GetMouseButton(0))
{
scroll_pos = scrollbar.GetComponent<Scrollbar> ().value;
}
else
{
for(int i = 0; i < pos.Length; i++)
{
if(scroll_pos < pos[i] + (distance / 2) && scroll_pos > pos[i] - (distance / 2))
{
scrollbar.GetComponent<Scrollbar> ().value = Mathf.Lerp(scrollbar.GetComponent<Scrollbar> ().value, pos[1], 0.15f);
}
}
}
}
}
movie002.gif
(335.9 kB)
Comment
Best Answer
Answer by yummy81 · Apr 23, 2020 at 04:12 PM
Look at the line of code, where you have your Mathf.Lerp method:
scrollbar.GetComponent<Scrollbar> ().value = Mathf.Lerp(scrollbar.GetComponent<Scrollbar> ().value, pos[1], 0.15f);
Replace it like that:
scrollbar.GetComponent<Scrollbar> ().value = Mathf.Lerp(scrollbar.GetComponent<Scrollbar> ().value, pos[i], 0.15f);