How can i move array of objects from position to position only when clicking on a key ?

In the Start:

void Start()
    {
        CreateRandomSpeedArray();

        formation = Formation.Square;
        ChangeFormation();

        foreach (Transform child in gameObject.transform)
        {
            if (child.tag == "Squad Member")
                squadMembers.Add(child.gameObject);
        }
    }

In the Update:

void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            if (destroySquad == true)
            {
                GameObject[] objects = GameObject.FindGameObjectsWithTag("Squad Member");
                if (objects.Length > 0)
                {
                    foreach (GameObject obj in objects)
                        Destroy(obj);
                }
            }

            ChangeFormation();
        }

        if (move == true)
            MoveToNextFormation();
    }

Then the MoveToNextFormation method:

private void MoveToNextFormation()
    {
        CreateRandomSpeedArray();
        float step = moveSpeed * Time.deltaTime;
        for (int i = 0; i < squadMembers.Count; i++)
        {
            squadMembers[i].transform.LookAt(newpos[i]);
            if (randomSpeed == true)
            {
                squadMembers[i].transform.position =
            Vector3.MoveTowards(squadMembers[i].transform.position, newpos[i], randomSpeeds[i] * Time.deltaTime);
            }
            else
            {
                squadMembers[i].transform.position =
                Vector3.MoveTowards(squadMembers[i].transform.position, newpos[i], step);
            }
            if (Vector3.Distance(squadMembers[i].transform.position, newpos[i]) < threshold)
            {
                if (squareFormation == true)
                {
                    Vector3 degrees = new Vector3(0, -90f, 0);
                    Quaternion quaternion = Quaternion.Euler(degrees);
                    squadMembers[i].transform.rotation = Quaternion.Slerp(squadMembers[i].transform.rotation, quaternion, rotateSpeed * Time.deltaTime);
                }
                else
                {
                    squadMembers[i].transform.rotation = Quaternion.Slerp(squadMembers[i].transform.rotation, qua[i], rotateSpeed * Time.deltaTime);
                }
            }
        }
    }

In the Start i’m calling ChangeFormation(); and inside ChangeFormation(); i’m setting the variable move to true.

Then in the update i’m checking if move is true and then calling MoveToNextFormation.
The problem is that i need MoveToNextFormation to be in the Update it self since i want to move the objects but i also want that only if i click on the F key it will call MoveToNextFormation and not all the time.
Since move is true it’s calling in the Update all the time to MoveToNextFormation and i want that only when i click on F call MoveToNextFormation and move the objects.

When the objects finished move and finished rotating inside the MoveToNextFormation method then don’t call MoveToNextFormation again until i click F again.

I tried to add move = false; in the bottom of MoveToNextFormation but then the objects/characters are not moving at all.

Confused…if you want it to trigger off of F, why not put it within the if check that you have for F?

You may have to use a coroutine to handle the move itself though, or some sort of loop.

You don’t want it at the end of ChangeFormation, you want it in one of those if clauses that decide when it’s finished. If there isn’t one in there that does that, you have to create one.