- Home /
Smooth scroll snap?
I currently have my menu working almost perfectly except I want to enable smooth scroll snapping to each element in the menu. I currently have a script that lerps the scrollbar value to the nearest snap point when the user isn't scrolling but although this is quite clean, it makes the user have to drag a distance bigger than is comfortable, and I just want a swipe to take them to the next one (it's not necessary for a big swipe to take them multiple items down, but that would be nice)
Below is the script I'm using right now, and a demo I found that is exactly what I want (but it's CSS so not really transferable):
What I want: http://codepen.io/sdras/pen/43c9d13b23bc34a85bb3a5e2ea985958
This script is attached to both the scrollbar to control the value, and the ScrollRect to get access to whether it is scrolling or not, and a static value to sync the values between instances:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UI_SmoothScrollSnap : MonoBehaviour, IBeginDragHandler, IEndDragHandler {
public float SnapSpeed = 10;
public float ScrollVelocity;
[ShowInInspector] //I added this outside of my IDE so not sure if it's the right attribute
private bool isDragging;
public int Snaps;
public static bool Dragging;
private Scrollbar scroll;
private ScrollRect sRect;
// Use this for initialization
void Start () {
scroll = FindObjectOfType<Scrollbar> ();
sRect = FindObjectOfType<ScrollRect> ();
}
// Update is called once per frame
void Update () {
isDragging = Dragging;
if (!scroll)
return;
//ScrollVelocity = sRect.inertia.y;
if (!Dragging)
scroll.value = Mathf.Lerp (scroll.value, Mathf.Round (scroll.value * (Snaps - 1)) / (Snaps - 1), SnapSpeed * Time.deltaTime);
}
public void OnBeginDrag (PointerEventData eventData) {
Dragging = true;
}
public void OnEndDrag (PointerEventData eventData) {
Dragging = false;
}
}
Answer by ExtinctSpecie · Mar 05, 2017 at 03:30 PM
check this out it may help you
This looks perfect! Give me a bit of time to test it before accepting the answer.
This has to be a joke. 20 pages of script for just scroll snapping. This is the worst code I have ever seen in my life.
Yes, 20 pages of commented, well documented code. Which is not only clean but also perfectly solves my problem.
If you're gonna come here and judge people's answers, then at least provide a better alternative yourself
https://www.youtube.com/watch?v=9B7ahj1kaYs
Here is your reply. Just 1 page of code for a simple thing that shouldn't be taking more than 20 $$anonymous$$utes to do or understand.
"shouldn't be taking more than 20 $$anonymous$$utes to do or understand" yet you link a 20 $$anonymous$$ute video (which is only part 1) plus no link to a repo or source code. I'll stick with the correct answer which is the one given by ExtinctSpecie
Answer by RigbyShows · Jul 30, 2018 at 07:56 AM
https://www.youtube.com/watch?v=9B7ahj1kaYs
Here is your reply. Just 1 page of code for a simple thing that shouldn't be taking more than 20 minutes to do or understand.
Answer by Vishwa001 · Sep 29, 2017 at 10:26 AM
Scroll Snap Unity for Horizontal scroll https://github.com/BushidoInteractive/ScrollSnapUnity is esay to use unity package and also manageable with your needs.
Your answer
Follow this Question
Related Questions
Scroll Rect Min/Max Size 0 Answers
ScrollRect scrollbar drag being overridden and stuttering to top 0 Answers
Where can I edit UI.ScrollView? 0 Answers
ScrollBar on ScrollRect not scrolling to the bottom when setting value through code 0 Answers
How do I Make TextMeshPro InputField scroll through code? 0 Answers