playMaker

Author Topic: (LeanTween) For loop is moving all objects at once instead of one by one  (Read 1801 times)

donchico

  • Playmaker Newbie
  • *
  • Posts: 1
Hello, I am trying to loop a list of objects (Ads) in order to move them one after another to their target position (which is Vector3(0,0,0)). The issue is that they all move at the same time instead of individually. Not sure what I am doing wrong and its been a couple hours of searching forums and experimenting with foreach loops and coroutines. Any help is greatly appreciated.

Script 1 (AdStartBoard)
Code: [Select]
//////////////////// 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));
                }
            }

   

Script 2 - Ad Tweener
Code: [Select]
//////////////////////// 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
                   
                }
            }
   


« Last Edit: November 20, 2020, 02:37:56 AM by donchico »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: (LeanTween) For loop is moving all objects at once instead of one by one
« Reply #1 on: November 20, 2020, 05:33:03 AM »
Hi.
Best to ask on unity forums as these are not playmaker actions

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 773
Re: (LeanTween) For loop is moving all objects at once instead of one by one
« Reply #2 on: November 20, 2020, 04:12:52 PM »
Perhaps you're using the same ID type for all ads so all tweens get launched at the same time?
Maybe you will need to have a single tween, with its own ID (based on name or game object for example) on each ad. Use a prefab then.

Or perhaps you're not waiting for a tween to have ended before launching the next.