Rotate around in formation relative to the lead unit
I have a formation setup for a multiple squads. There is one main leader that the other sub squad leaders are offset by. Each of the sub squads units are offset by there squad leader. So say if i have five squads, I'll have one main leader and four sub squad leaders that are offset by the main leader. Then I would have each squad members offset by each squad leader (including the squad for the main leader)
public void SetFormations(SrtsEnums.Formation formation, List<AbstractShip> thisGroup, Vector3 loc)
{
int i = 0;
bool init = true;
int k = 0;
AbstractShip subgroupLeader = null;
AbstractShip groupLeader = null;
float shipDist = 3;
float subgroupDistX = 10f;
float subgroupDistY = 5f;
float subgroupDistZ = 7.5f;
foreach (AbstractShip ship in thisGroup)
{
if (ship.gameObject.activeSelf)
{
if (init)
{
//ship.transform.rotation;
ship.PosOffset = Vector3.zero;
ship.ShipToOffset = ship;
ship.transform.position = loc;
subgroupLeader = groupLeader = ship;
init = false;
}
else
{
switch (formation)
{
case SrtsEnums.Formation.None:
break;
case SrtsEnums.Formation.Delta:
if (i == 5) //each subgroup is 5 ships
{
ship.ShipToOffset = groupLeader;
subgroupLeader = ship;
switch (k)
{
case 0:
ship.PosOffset = new Vector3(-subgroupDistX, subgroupDistY, -subgroupDistZ); //back left up
break;
case 1:
ship.PosOffset = new Vector3(subgroupDistX, subgroupDistY, -subgroupDistZ); //back right up
break;
case 2:
ship.PosOffset = new Vector3(-subgroupDistX, -subgroupDistY, -subgroupDistZ); //back left down
break;
case 3:
ship.PosOffset = new Vector3(subgroupDistX, -subgroupDistY, -subgroupDistZ); //back right down
break;
case 4:
ship.PosOffset = new Vector3(-subgroupDistX, subgroupDistY, -(subgroupDistZ * 3)); //back left up
break;
case 5:
ship.PosOffset = new Vector3(subgroupDistX, subgroupDistY, -(subgroupDistZ * 3)); //back right up
break;
case 6:
ship.PosOffset = new Vector3(-subgroupDistX, -subgroupDistY, -(subgroupDistZ * 3)); //back left down
break;
case 7:
ship.PosOffset = new Vector3(subgroupDistX, -subgroupDistY, -(subgroupDistZ * 3)); //back right down
break;
case 8:
ship.PosOffset = new Vector3(0, 0, -(subgroupDistZ * 4)); //back left
break;
}
k++;
i = 0;
}
else
{
ship.ShipToOffset = subgroupLeader;
switch (i)
{
case 1:
ship.PosOffset = new Vector3(-shipDist, 0, -shipDist); //back one z, to the left one x, y = subgroup leader
break;
case 2:
ship.PosOffset = new Vector3(shipDist, 0, -shipDist); //back one z, to the left one x, y = subgroup leader//back one z, to the right one x, y = subgroup leader
break;
case 3:
ship.PosOffset = new Vector3(-(shipDist * 2), 0, -(shipDist * 2)); //back one z, to the left one x, y = subgroup leader
break;
case 4:
ship.PosOffset = new Vector3((shipDist * 2), 0, -(shipDist * 2)); //back one z, to the left one x, y = subgroup leader//back one z, to the right one x, y = subgroup leader
break;
}
}
break;
}
}
ship.LeadShip = groupLeader;
ship.transform.position = ship.ShipToOffset.transform.position + ship.PosOffset;
i++;
}
}
}
The problem I have is that I need the formation to rotate with and around the Main Leader so that it is always at the head of the movement. What they are doing now is that they are sticking to the formation and once in it they keep it, regardless of the orientation of the main leader.
public void MoveInFormation(Vector3 dest)
{
foreach (AbstractShip ship in ThisGroup)
{
if (ship.LeadShip == ship)
{
if (ship.HasArrived(dest))
{
initMoveInFormation = true;
}
{
if (initMoveInFormation)
{
initMoveInFormation = false;
SrtsEnums.Formation _thisFormation = SrtsEnums.Formation.None;
if (GetComponentInChildren<AbstractFighter>() != null || GetComponentInChildren<AbstractCorvette>() != null)
{
_thisFormation = SrtsEnums.Formation.Delta;
}
SetFormations(_thisFormation, ThisGroup, transform.position);
}
}
}
ship.MoveAndTurn(dest + ship.ShipToOffset.transform.position + ship.PosOffset);
}
}
I have them all in a game object and can rotate the game object to rotate them all but I am trying to avoid this solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving relative to camera 0 Answers
How would I orient VTOL engines relative to the direction of a ship's movement? 1 Answer
Face direction of a Vector 3 1 Answer
Does a game object referenced in-script, automatically become a "local" origin point? 0 Answers
Transform.Translate not moving in the direction of specified vector 2 Answers