Dears
As a beginner for a quiet simple project I am attempting to replicate the animation embedded to move an array of panels.
wj3nbw
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?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovimentoBase2 : MonoBehaviour
{
// Start is called before the first frame update
public GameObject[] layer;
private Vector2 posAttuale;
private Vector2 posSuccessiva;
public float dimensione = 192;
private int totaleLivelli;
private int x = 0;
private int i = 0;
//private bool shouldLerp = false;
private float timeStartedLerping;
public float lerpTime;
//private bool stop = false;
// this coroutine will set the timer
IEnumerator StartLerping()
{
timeStartedLerping = Time.time;
yield return null;
}
void Start()
{
StartCoroutine(StartLerping());
totaleLivelli = layer.Length;
}
// in the upodate there is a check for the timer to set coroutines
void Update()
{
if (Time.time - timeStartedLerping <= lerpTime)
{
StartCoroutine(MuoviTutti());
StopCoroutine(FermiTutti());
}
else
{
StartCoroutine(FermiTutti());
StopCoroutine(MuoviTutti());
}
}
//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
IEnumerator MuoviTutti()
{
for (x = 0; x < totaleLivelli; x++)
{
posAttuale = layer[x].GetComponent<RectTransform>().anchoredPosition;
posSuccessiva = new Vector2(layer[x].GetComponent<RectTransform>().anchoredPosition.x, layer[x].GetComponent<RectTransform>().anchoredPosition.y + dimensione);
layer[x].GetComponent<RectTransform>().anchoredPosition = Lerp(posAttuale, posSuccessiva, timeStartedLerping, lerpTime);
if (layer[x].GetComponent<RectTransform>().anchoredPosition.y >= 185)
{
layer[x].GetComponent<RectTransform>().anchoredPosition = new Vector2(layer[x].GetComponent<RectTransform>().anchoredPosition.x, - dimensione * (totaleLivelli - 1));
}
}
yield return new WaitForSeconds(lerpTime);
}
//This coroutine will stop the animation and move the panels to the nearest right position
IEnumerator FermiTutti()
{
for (x = 0; x < totaleLivelli; x++)
{
posAttuale = layer[x].GetComponent<RectTransform>().anchoredPosition;
for (i=0; i < totaleLivelli; i++)
{
if (posAttuale[1] < 0 - (dimensione/2) * i && posAttuale[1] > - (dimensione/2) - (dimensione / 2) * i)
{
posSuccessiva = new Vector2(0, - dimensione * i);
layer[x].GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(posAttuale, posSuccessiva, 1);
}
}
}
yield return new WaitForSecondsRealtime(3f);
yield return StartCoroutine(StartLerping());
}
// Vector for Linear interpolation
Vector2 Lerp(Vector2 start, Vector2 end, float timeStartedLerping, float lerpTime = 1)
{
float timeSinceStarted = Time.time - timeStartedLerping;
float percentageComplete = timeSinceStarted / lerpTime;
var result = Vector2.Lerp(start, end, percentageComplete);
return result;
}
}