Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Box Formation

Discussion in 'Scripting' started by boco, Dec 4, 2012.

  1. boco

    boco

    Joined:
    Dec 7, 2010
    Posts:
    373
    Hello I seem to be having some great deal of trouble making a box formation in game.

    here is a code snippet of what I'am doing I just need help with logic to get it in a box formation.. Been working on this for the past 2 days and I cannot seem to get them in a box.. only Echelon Left.

    Code (csharp):
    1.     void CreateFormation()
    2.     {
    3.         //Anchor the first position
    4.         SquadPositions[0] = Center.transform.position;
    5.  
    6.         //Increment the currRow
    7.         for (int j = 1; j < SquadPositions.Length; j++)
    8.         {
    9.             //Next Row
    10.             SquadPositions[j] = new Vector3(SquadPositions[0].x,
    11.               100, SquadPositions[0].z + (j * flockSeperation));
    12.  
    13.             for (int k = 0; k < columns; k++)
    14.             {
    15.                 SquadPositions[j] = new Vector3(SquadPositions[j - 1].x  
    16.                        + (k * flockSeperation)
    17.                        , 100, SquadPositions[0].z + (j * flockSeperation));
    18.             }
    19.  
    20.             RaycastHit hit;
    21.             if (Physics.Raycast(SquadPositions[j], -Vector3.up, out hit))
    22.             {
    23.                 //Set the height of the squad position
    24.                 SquadPositions[j] = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    25.  
    26.             }
    27.         }
    28.  
    29.  
    30.     }
     
    Last edited: Dec 4, 2012
  2. boco

    boco

    Joined:
    Dec 7, 2010
    Posts:
    373
    Never Mind I finally got it...
     
  3. unity_SqKRi3Klpu8M1w

    unity_SqKRi3Klpu8M1w

    Joined:
    Oct 21, 2019
    Posts:
    11
    I know it's to late. Can you share me this algorithm?
    I am needing arrange object in a box formation like you :(( thanks!
     
  4. boco

    boco

    Joined:
    Dec 7, 2010
    Posts:
    373
    Hey this was EONS ago but I think I can come up with something by the end of the week for you, I have a few algorithms laying around and what are you trying to-do this for? Unity's Built-in Nav Mesh system or custom positioning system?
     
  5. boco

    boco

    Joined:
    Dec 7, 2010
    Posts:
    373
    Though I think for this exact script I don't have access to any longer with old company... Let me know what you need done and I can help you out.
     
  6. unity_SqKRi3Klpu8M1w

    unity_SqKRi3Klpu8M1w

    Joined:
    Oct 21, 2019
    Posts:
    11
    Thanks for your reply. I found a algorithm to make a crowd to be square formation. But can't find any algorithm to make crowd fill a circle formation (not a ring).
    Do you have any idea?
     
  7. PanzerSerB

    PanzerSerB

    Joined:
    Aug 8, 2021
    Posts:
    1
    Bro, how to make objects move like Total War? And the formation is dynamic.
    thx