scrollRect wrap+inertia issues

Hi :slight_smile:

What is this?

  • I have implemented a component with inertia+wrap. It has some other stuff, but I’ve ripped it out to isolate the bug.

  • The bug is: sometimes the inertia velocity calculation goes wrong and it’s too high, so high it looks still for a second (because it’s wrapping). It slows down properly.

  • I can’t use ScrollRect’s inertia because it doesn’t play nice with the stuff that’s not seen here.

  • 3 weeks on this bug (I only have a couple of hours per week to look at it, though).

How to reproduce

  • add a component with the provided code to a working scrollRect (horizontal, no inertia, unbounded).

  • verify inertia and wrapping at edges work (wrapping ensures the scrollRect is always filled by content).

  • Drag and drop AT the wrapping edge. When done “correctly”, the inertia will bee too damn high (it slides at a speed of one scrollRect width per frame, slowing down as expected)

Why does it happen

  • When the content wraps, comparing the position with the previous position yields almost the entire width of the scrollRect.

What am I looking for

  • Inertia should not flip out at the edges, as it currently does.

  • I tried to de-wrap the speed, but ran into confusing timing issues (OnEndDrag vs Update vs LateUpdate) that fixed half the cases and broke the other half, increasing the speed when it was OK on one case and not doing anything when it needed to be fixed in the other.

  • I could never get a fix that always worked and ran out of ideas.

  • I will not taint your feedback with my failed fixes.

Show some code

  • Thanks for reading this far. Here is the code for test.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class test : MonoBehaviour, IInitializePotentialDragHandler, IEndDragHandler {
    public bool sliding = false;
    public float inertialDamping = 5F;
    Vector2 inertiaSpeed = new Vector2(0,0);
    public Vector2 lastPos = new Vector2(0,0);
    public Vector2 secondLastPos = new Vector2(0,0);
    public Vector2 wrapCount = new Vector2 (0, 0);

    public void OnInitializePotentialDrag (PointerEventData eventData) {
        sliding = false;
        wrapCount = new Vector2 (0, 0);
    }

    public void OnEndDrag (PointerEventData eventData) {
        sliding = true;
        inertiaSpeed = (lastPos - secondLastPos );
        inertiaSpeed /= Time.deltaTime;
    }

    void Update () {
        ScrollRect scrollRect = gameObject.GetComponent<ScrollRect> ();
        secondLastPos = lastPos;
        lastPos = scrollRect.normalizedPosition;
     
        if (sliding) {
         
            float updatedMagnitude = inertiaSpeed.magnitude-(Time.deltaTime*inertialDamping*inertialDamping);
            if (updatedMagnitude<0) {
             
                updatedMagnitude = 0;
            }
            inertiaSpeed = inertiaSpeed.normalized*updatedMagnitude;
            scrollRect.normalizedPosition+=inertiaSpeed*Time.deltaTime;
        }

        wrapCount.x = 0;
        if (scrollRect.horizontalNormalizedPosition<0) {
            wrapCount.x = -1+(int)scrollRect.horizontalNormalizedPosition;
        }
     
        if (scrollRect.horizontalNormalizedPosition>=1) {
            wrapCount.x = (int)scrollRect.horizontalNormalizedPosition;
        }
        scrollRect.normalizedPosition = scrollRect.normalizedPosition - wrapCount;

    }
}

For anyone who reads this, I ended up fixing it by doing everything in LateUpdate instead. Turns out values were being changed by a LateUpdate in ScrollRect, overwriting my own changes.

I’m amazed that some one else out there also needed to wrap a ScrollRect, and has a working solution. Thanks!