Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Struggle with a simple vertical animation

Discussion in 'Scripting' started by jdenaro, Jun 16, 2022.

  1. jdenaro

    jdenaro

    Joined:
    Nov 25, 2021
    Posts:
    4
    Dears
    As a beginner for a quiet simple project I am attempting to replicate the animation embedded to move an array of panels.

    I am using coroutines in the update methods but the effect I am obtaining is not going to that direction, even if in my logic it should work...
    Have you any suggestion to help me?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class MovimentoBase2 : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.  
    10.     public GameObject[] layer;
    11.     private Vector2 posAttuale;
    12.     private Vector2 posSuccessiva;
    13.  
    14.     public float dimensione = 192;
    15.  
    16.     private int totaleLivelli;
    17.     private int x = 0;
    18.     private int i = 0;
    19.  
    20.     //private bool shouldLerp = false;
    21.  
    22.     private float timeStartedLerping;
    23.     public float lerpTime;
    24.  
    25.     //private bool stop = false;
    26.  
    27.  
    28.  
    29.  
    30.     // this coroutine will set the timer
    31.     IEnumerator StartLerping()
    32.     {
    33.         timeStartedLerping = Time.time;
    34.        
    35.         yield return null;
    36.     }
    37.  
    38.  
    39.  
    40.  
    41.  
    42.     void Start()
    43.     {  
    44.         StartCoroutine(StartLerping());
    45.         totaleLivelli = layer.Length;
    46.     }
    47.  
    48.  
    49.  
    50.     // in the upodate there is a check for the timer to set coroutines
    51.     void Update()
    52.     {
    53.         if (Time.time - timeStartedLerping <= lerpTime)
    54.         {
    55.             StartCoroutine(MuoviTutti());
    56.             StopCoroutine(FermiTutti());
    57.         }
    58.         else
    59.         {
    60.             StartCoroutine(FermiTutti());
    61.             StopCoroutine(MuoviTutti());
    62.         }
    63.     }
    64.  
    65.    
    66.  
    67.  
    68.  
    69.     //this coroutine should move each panel vertically to the next position. if the panel goes on top of the window it will go under all the other layers
    70.     IEnumerator MuoviTutti()
    71.     {
    72.         for (x = 0; x < totaleLivelli; x++)
    73.         {
    74.             posAttuale = layer[x].GetComponent<RectTransform>().anchoredPosition;
    75.             posSuccessiva = new Vector2(layer[x].GetComponent<RectTransform>().anchoredPosition.x, layer[x].GetComponent<RectTransform>().anchoredPosition.y + dimensione);
    76.  
    77.             layer[x].GetComponent<RectTransform>().anchoredPosition = Lerp(posAttuale, posSuccessiva, timeStartedLerping, lerpTime);
    78.  
    79.             if (layer[x].GetComponent<RectTransform>().anchoredPosition.y >= 185)
    80.             {
    81.                 layer[x].GetComponent<RectTransform>().anchoredPosition = new Vector2(layer[x].GetComponent<RectTransform>().anchoredPosition.x, - dimensione * (totaleLivelli - 1));
    82.             }
    83.         }
    84.         yield return new WaitForSeconds(lerpTime);
    85.     }
    86.  
    87.  
    88.  
    89.  
    90.  
    91.     //This coroutine will stop the animation and move the panels to the nearest right position
    92.     IEnumerator FermiTutti()
    93.     {
    94.         for (x = 0; x < totaleLivelli; x++)
    95.         {
    96.             posAttuale = layer[x].GetComponent<RectTransform>().anchoredPosition;
    97.             for (i=0; i < totaleLivelli; i++)
    98.             {
    99.                 if (posAttuale[1] < 0 - (dimensione/2) * i && posAttuale[1] > - (dimensione/2) - (dimensione / 2) * i)
    100.                 {
    101.                     posSuccessiva = new Vector2(0, - dimensione * i);
    102.                     layer[x].GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(posAttuale, posSuccessiva, 1);
    103.                 }
    104.             }
    105.         }
    106.         yield return new WaitForSecondsRealtime(3f);
    107.         yield return StartCoroutine(StartLerping());
    108.     }
    109.  
    110.  
    111.     // Vector for Linear interpolation
    112.     Vector2 Lerp(Vector2 start, Vector2 end, float timeStartedLerping, float lerpTime = 1)
    113.  
    114.     {
    115.         float timeSinceStarted = Time.time - timeStartedLerping;
    116.         float percentageComplete = timeSinceStarted / lerpTime;
    117.         var result = Vector2.Lerp(start, end, percentageComplete);
    118.         return result;
    119.     }
    120.  
    121. }
    122.  
    123.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Just use an animation... ? That's kinda what it's for... our artists (most of whom are not engineers) use animations all day long every day to make just about every piece of our games wiggle and move and slide and spin...

    If you insist on doing it in code, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494