Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Unity ScrollRect bugs out after i move it using a function

Discussion in 'UGUI & TextMesh Pro' started by Intraterrestrial_, Oct 23, 2023.

  1. Intraterrestrial_

    Intraterrestrial_

    Joined:
    Jun 27, 2021
    Posts:
    20
    I have created a swipe script that moves two ScrollRects with touch, but for some reason a weird bug happpens sometimes, i haved seen the two ScrollRects work fine but sometime the phone freezes for less than a second and thats when the bug happens, i am not sure what it is, any help would be appreciated.
    This is a link to a video where it shows how it is the bug.
    https://drive.google.com/file/d/1ITH_6JlRRVs1YTxBQezzIPKmNUjtSaxD/view?usp=share_link
    Thanks.
    Code (CSharp):
    1. void Update()
    2. {
    3.        Swipe();
    4. }
    5.  
    6. private Vector3 fp;
    7. private Vector3 lp;
    8.  
    9. if (Input.touchCount == 1)
    10.         {
    11.             Touch touch = Input.GetTouch(0);
    12.             if (touch.phase == TouchPhase.Began)
    13.             {
    14.                 fp = touch.position;
    15.                 lp = touch.position;
    16.             }
    17.             else if (touch.phase == TouchPhase.Ended)
    18.             {
    19.                 lp = touch.position;
    20.                 if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
    21.                 {
    22.                     if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
    23.                     {
    24.                         if ((lp.x > fp.x))
    25.                         {   //Right swipe
    26.    StartCoroutine(SmoothMove(transPP.transform.localPosition, firstLocation, easing));
    27.                         }
    28.                         else
    29.                         {   //Left swip
    30. StartCoroutine(SmoothMove(transPP.transform.localPosition, new Vector3(-vs.widthSF, 0, 0), easing));
    31.                         }
    32.                     }
    33.                 }
    34.             }
    35.      
    36. IEnumerator SmoothMove(Vector3 startpos, Vector3 endpos, float seconds)
    37.     {
    38.         float t = 0f;
    39.         while (t <= 1f)
    40.         {
    41.             float deltaTime = Mathf.Max(Time.deltaTime, 0.015f);
    42.             if (float.IsNaN(deltaTime))
    43.             {
    44.                 deltaTime = 0.015f;
    45.             }
    46.             t += deltaTime / seconds;
    47.             transPP.transform.localPosition = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0f, 1f, t));
    48.             yield return null;
    49.         }
    50.     }
    The NAN verification is the only thing i found on the internet that could cause this problem.
     
    Last edited: Nov 9, 2023