0
\$\begingroup\$

Now it will move all the objects of allLines and instancesToMove at the same time. But I want to take each time one object from the allLines and one from the instancestoMove and move this two together then the next two objects and so on.

if (animateLines)
        {
            counter++;
            for (int i = 0; i < allLines.Count; i++)
            {

                endPos = allLines[i].GetComponent<EndHolder>().EndVector;
                Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
                Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);

                allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);

                instancesToMove[i].transform.position =
                    Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);

                if (Vector3.Distance(instancesToMove[i].transform.position, endPos) < 0.1f)
                {
                    DestroyImmediate(instancesToMove[i]);
                    instancesToMove.RemoveAt(i);
                    DestroyImmediate(allLines[i]);
                    allLines.RemoveAt(i);
                }
            }
        }

Both allLines and instancesToMove are List

\$\endgroup\$
  • \$\begingroup\$ Can you tell us more about how you want to sequence these pairs of movement? Should there be a delay between moving the first pair and moving the second? How should the pairs be chosen? In general, the more you can tell us about the context — what's the player-facing gameplay feature that this is serving — the clearer it will be how to help you meet those needs without a ton of follow-up questions. \$\endgroup\$ – DMGregory Feb 14 '18 at 15:35
  • \$\begingroup\$ @DMGregory There are twl ists: allLines and instancesToMove what I want to do is to take each time one item from each List. Index 0 from allLines and index 0 from instancesToMove and move this two objects at the same time like I'm doing now but each time moving two objects from the lists. First index 0 when they finished moving take from each list the index 1 and move the two objects finished moving ? take index 2 from bothl ists and so on. The idea is to move them like i'm doing now but the sequence should be each time to take the next index from both lists and move the two objects. \$\endgroup\$ – Daniel Lip Feb 14 '18 at 15:46
  • \$\begingroup\$ And to wait for the two objects to finish moving then start to move the next two wait for finish move the next two. \$\endgroup\$ – Daniel Lip Feb 14 '18 at 15:46
0
\$\begingroup\$
int index = 0; //you must persist index state
Vector3 endpos;
bool isMoving = false; //same as your "animateLines"

void BeginMove(int i)
{
    endPos = allLines[i].GetComponent<EndHolder>().EndVector; //init
    isMoving = true;
}

void Move(int i )
{
    Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
    Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);

    allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);

    instancesToMove[i].transform.position =
        Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);

    if (Vector3.Distance(instancesToMove[i].transform.position, endPos) < 0.1f)
    {
        EndMove(i); //deinit
        isMoving = false;
    }
}

void EndMove(int i)
{
    DestroyImmediate(instancesToMove[i]);
    instancesToMove.RemoveAt(i);
    DestroyImmediate(allLines[i]);
    allLines.RemoveAt(i);

    index++;
}

void Update()
{
    if (isMoving)
    {
        Move(index);
    }
    else //(try to) start moving the next pair
    {
        if (Input.GetKeyDown(KeyCode.Space)) //comment this line if you want it to proceed automatically
            if (index < allLines.Count)
                BeginMove(index);
    }
}

Maybe not perfect, but that should give you the idea.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.