Segmented Formation
This page contains a code example to explain how to use FormationLayoutProvider to provide positions for different unit types in a single formation. This would be suitable for situations where different unit types like archers and cavalry can assume the same formation, but grouped with their own spacing and other properties (infantry front, then archers, cavalry and finally siege weapons).
If you have the package already, check the MovingUnitFormation scripts which already provides segmented formations and you can use that or copy the parts you find useful.
private FormationLayoutProvider layoutProvider;
private void Awake()
{
// Create an instance of formation provider with either regular positions
// or formation slots.
layoutProvider = new FormationLayoutProvider(CreateFormationSlots);
}
// Single formation
private Vector3[] CreateFormationPositions(int count)
{
return CurrentFormation.GetPositions(count);
}
// Segmented formation (multiple formations)
private FormationSlot[] CreateFormationSlots(FormationSlotRequest[] slotRequests)
{
// Create formation group for each type requested by the system/manager.
var groups = slotRequests.Select(request => new GroupingRectFormation.Group
{
// Specify custom column count and spacing for each group
ColumnCount = 6,
Spacing = 2,
Type = request.Type,
UnitCount = request.Count
});
// Use GroupingRectFormation to create the segmented formation
// (internally using RectangleFormation)
var slots = GroupingRectFormation.CreatePositions(groups.ToArray());
// Convert slots to the expected type.
return slots.Select(slot => new FormationSlot(slot.type, slot.position)).ToArray();
}Last updated