So I'm working on an RTS game and I'm trying to set pre-defined positions for units using my own formula. It takes the list of units and makes a position for each one in an array. The array then goes through a for loop which offsets each x position so units don't collide. The problem is that the Y and Z components are set to 0 whenever I try to get them to move. I'm not sure why because I'm not touching them.

Here it is:

Selected units list if you needed it

    public static List<GameObject> SelectedUnits;
    void MakeFormation(RaycastHit hit)
        Vector3 origin = hit.point;
        float Separation = 10f;
        Vector3[] positions = new Vector3[SelectedUnits.Count];

        for(int i = 0; i < positions.Length; i++)
            positions[i].x = origin.x+Separation*i;
        MultiSelect(positions, origin);

And the function that actually gets the units going

    void MultiSelect(Vector3[] positions, Vector3 origin)
        if (SelectedUnits.Count > 0)
            for (int i= 0; i < positions.Length; i++)
                Debug.Log("Moving group to area: " + positions[i].ToString("F4"));

            for(int i=0; i< SelectedUnits.Count;i++)

                Debug.Log("Index: " + i);
  • 1
    \$\begingroup\$ Where do you initialize your y and z coordinates to something other than zero? \$\endgroup\$ – DMGregory Nov 26 '19 at 18:33
  • \$\begingroup\$ In another part of the code however I have not changed anything there and it works perfectly there as well. \$\endgroup\$ – Kiyo Nov 26 '19 at 21:54
  • \$\begingroup\$ I just remembered where Ive gone wrong. Im sorry, I forgot to store the y and z components in the array as well. I will update. \$\endgroup\$ – Kiyo Nov 26 '19 at 21:55

Your Answer

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

Browse other questions tagged or ask your own question.