For循环一次移动所有对象而不是一个一个地移动

For loop is moving all objects at once instead of one by one

我正在尝试循环对象列表 (Ads),以便将它们一个接一个地移动到它们的目标位置(即 Vector3(0,0,0))。问题是它们都是同时移动的,而不是单独移动的。不确定我做错了什么,我花了几个小时搜索论坛并试验 foreach 循环和 IEnumerators。非常感谢任何帮助。

脚本 1 (AdStartBoard)

//////////////////// Script 1 (AdStartBoard) /////////////////////////////
// This first script works, I just posted it for background information
// Script#2 is below and is the one giving me issues
public List<GameObject> ads = new List<GameObject>();
public List<RectTransform> adRectTransforms = new List<RectTransform>();
public List<SpriteRenderer> adSpriteRenderers = new List<SpriteRenderer>();
public List<Vector3> adStartingPosition = new List<Vector3>();
public List<float> adStartingRotation = new List<float>();
private List<Vector2> adStartingSize = new List<Vector2>();

// At Start, 7 ads will be randmly placed across a canvas background 
private void Start()
        {
            
            DeterminePositionsRow();
            for (int i = 0; i < transform.childCount; i++)
            {
                LeanTween.move(adRectTransforms[i], adStartingPosition[i], timeToStart);
                LeanTween.rotateAround(ads[i], Vector3.forward, adStartingRotation[i], timeToStart);
                adSpriteRenderers[i].size = adStartingSize[i];
                LeanTween.alpha(ads[i], 1f, timeToStart);
            }       
        }

// This function will handle the random positions for the ads 
 public void DeterminePositionsRow()
        {
            int numAdsRowOne = (transform.childCount / 2);
            int numAdsRowTwo = transform.childCount - numAdsRowOne;
            float rowOneGap = (Screen.width * percentageScreenWidthToFill) / numAdsRowOne;
            float rowTwoGap = (Screen.width * percentageScreenWidthToFill) / numAdsRowTwo;

            for (int i = 0; i < transform.childCount; i++)
            {
                if (i < numAdsRowOne)
                {
                    adStartingPosition.Add(new Vector3((i + 1) * rowOneGap - ((rowOneGap * numAdsRowOne + averageAdWidth) / 2f) +       Random.Range(xMin, xMax), Random.Range(rowOneYMin, rowOneYMax), 0f));
                }
                else
                {
                    adStartingPosition.Add(new Vector3((i + 1 - numAdsRowOne) * rowTwoGap - ((rowTwoGap * numAdsRowTwo + averageAdWidth) / 2f) + Random.Range(xMin, xMax), Random.Range(rowTwoYMin, rowTwoYMax), 0f));
                }
                adStartingRotation.Add(Random.Range(rotateMin, rotateMax));
                float newSize = Random.Range(sizeMin, sizeMax);
                adStartingSize.Add(new Vector2(newSize, newSize));
            }
        }

脚本 2 - Ad Tweener

//////////////////////// Script 2 ///////////////////////////////////
AdStartBoard adStartBoardScript;
private List<Vector3> adInitalLocations = new List<Vector3>();
private List<float> adInitalRotation = new List<float>();
private List<RectTransform> adRectTransforms = new List<RectTransform>();
private List<GameObject> adObjects = new List<GameObject>();
private RectTransform currentAd;
private RectTransform previousAd;

private void Awake()
        {
            adStartBoardScript = GetComponent<AdStartBoard>();
            adInitalLocations = adStartBoardScript.adStartingPosition;
            adInitalRotation = adStartBoardScript.adStartingRotation;
            adRectTransforms = adStartBoardScript.adRectTransforms;
            adObjects = adStartBoardScript.ads;
            
        }

private void Start()
        {
            //Debug Tools to see if Script 2 gets the correct values from Script 1
            foreach (Vector3 pos in adInitalLocations)
            {
                Debug.Log("Starting Ad Position is: " + pos);
            }
           
            // Here I want to loop thru the Ads List to move the ads to its target Position 
            // one by one, but they all move at the same time instead.
            for(int i = 0; i < adStartBoardScript.ads.Count; i++)
            {
                LeanTween.move(adObjects[i], targetPosition, moveTime);
                // For now there is only 7 ads and I can get them to work if I do it manually...
                // (ad[0], ad[1], etc) but over time I will have dozens of ads I want to 
                // loop thru individually and I am unable to create a proper for loop to do so.
                
                // The main idea is to get the starting position from Script#1 and move the 
                // ad to target position in Script#2
                
                // The target position is center of screen (Vector3(0,0,0))
                
                // Once an individal Ad has moved to its target position, the goal is to have 
                // it move back to its original place from Script#1 (adStartingPosition) 
                // creating an infinite loop of ads rotating to center and back one after another.
                
                // any help is appreciated 
                
            }
        }

您在同一帧触发所有动作(Start())。 如果你的 tweener 允许延迟,你可以使用那个,否则你必须使用 Coroutine 来延迟触发它们:

private void Start()
{
    StartCoroutine(TriggerMovement(1));
}

private IEnumerator TriggerMovement(float delayPerObject)
{
    var wait = new WaitForSeconds(delayPerObject);
    for(int i = 0; i < adStartBoardScript.ads.Count; i++)
    {
        LeanTween.move(adObjects[i], targetPosition, moveTime);
        yield return wait;
    }
}

但是如果你想以乒乓方式移动它们,你可以看看 Mathf.PingPong。不要使用 tweener,只需添加一个自定义脚本来控制每个广告的广告移动,并让它们执行类似 transform.position = Vector3.Lerp(startPos, targetPos, Mathf.PingPong(Time.time + delay, 1)).

的操作