Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Give an infinite scroll extra push

Discussion in '2D' started by Fatamos, Jul 6, 2022.

  1. Fatamos

    Fatamos

    Joined:
    May 27, 2020
    Posts:
    12
    So I'm working on infinite scroll view and I'd like to add auto scroll effect when entering the scene. Something like this: https://media.giphy.com/media/xvTkLjm3RimAi7RgXp/giphy.gif All the movement I made with the mouse.

    How can I achieve this effect? I tried to get this effect with this:
    Code (CSharp):
    1. public IEnumerator AutoScroll(ScrollRect scrollRect, float startPos, float endPos, float duration)
    2.     {
    3.         yield return new WaitForSeconds(5f);
    4.         float t0 = 0.0f;
    5.         while (t0 < 1.0f)
    6.         {
    7.             t0 += Time.deltaTime / duration;
    8.             scrollRect.horizontalNormalizedPosition = Mathf.Lerp(startPos, endPos, t0);
    9.             yield return null;
    10.         }
    11.     }
    And with this:

    Code (CSharp):
    1. scrollRect.normalizedPosition = new Vector2(0, 1);
    but this only works if its normal scroll rect, not infinite. Any ideas on how can I achieve this effect? Thanks!
     
  2. Fatamos

    Fatamos

    Joined:
    May 27, 2020
    Posts:
    12
    That was actually pretty easy to achieve. I simply used scroll rect's velocity like this:
    Code (CSharp):
    1. _scrollRect.velocity += 2500 * Vector2.left;
    If you want to use this on vertical rect multiply it with Vector2.up