The systems I built in Unity for Royal Vermin, starting with Team Match.
Royal Vermin is a local party game where everyone fights for themselves. Team Match is a new match option that turns it into Red versus Blue, for 2 to 4 players, humans and bots mixed. Players keep the same game, but now a win belongs to a whole team.

Teams only exist during a match. In the lobby every player keeps their own color and can hit everyone, so nothing changes for people who never touch the option.
// Called when the match starts. Teams only exist between Begin and End.
public static void Begin(TeamMatchMode mode, IReadOnlyList<int> humanIds,
IReadOnlyList<int> botIds, FriendlyFireMode friendlyFire = FriendlyFireMode.On)
{
_friendlyFire = friendlyFire;
var humans = SortedIds(humanIds);
var bots = botIds?.ToList() ?? new List<int>();
var all = humans.Concat(bots).ToList();
// Two sides need at least two players.
var useTeams = mode == TeamMatchMode.On && all.Count >= 2;
if (useTeams) ApplyRoster(humans, bots, all);
SetActive(useTeams);
}The team rules live in plain C# classes with no Unity dependency (TeamAssignment, TeamPlan, TeamScoring, TeamSelectBoard). The Unity side only reads them. That made it possible to cover them with EditMode unit tests, and kept the menu code thin. By default, the first half of the players is red and the rest is blue, so a player who never opens the menu still gets a fair match.
// Default split: first half of the players is red, the rest is blue.
// 4 players -> 1+2 vs 3+4, 3 players -> 1+2 vs 3, 2 players -> 1 vs 2
public void ApplyDefault(IReadOnlyCollection<int> playerIds)
{
var sorted = Sanitize(playerIds).OrderBy(id => id).ToList();
var redCount = (sorted.Count + 1) / 2;
for (var i = 0; i < sorted.Count; i++)
_teams[sorted[i]] = i < redCount ? TeamId.Red : TeamId.Blue;
Changed?.Invoke();
}Players choose their sides in a dedicated menu, built to work with a controller only. Every player and bot is a card that starts in a middle column. One player drives the menu.

// Left / right moves the selected card one column: red, middle, blue.
public bool MoveSelectedCard(int direction)
{
if (CardCount == 0 || direction == 0) return false;
var current = (int)_columns[Selected];
var next = Math.Clamp(current + Math.Sign(direction), (int)TeamColumn.Red, (int)TeamColumn.Blue);
if (next == current) return false;
_columns[Selected] = (TeamColumn)next;
return true;
}
// The menu can only be refused when a team would be empty (4 vs 0).
public TeamBoardProblem GetProblem()
{
if (Count(TeamColumn.Middle) > 0) return TeamBoardProblem.Unplaced;
if (Count(TeamColumn.Red) == 0 || Count(TeamColumn.Blue) == 0) return TeamBoardProblem.EmptyTeam;
return TeamBoardProblem.None;
}Players do not have to place everyone. Pressing confirm sends the cards left in the middle to the smaller team, so a group in a hurry still gets a fair match.
// On confirm, cards left in the middle go to the smaller team, one by one.
// 1 vs 2 with one card left becomes 2 vs 2. On a tie, a coin flip decides.
var toRed = red < blue || (red == blue && random.Next(2) == 0);
_columns[card] = toRed ? TeamColumn.Red : TeamColumn.Blue;
if (toRed) red++;
else blue++;Parties often replay with the same people, so the placement is saved between sessions. Humans are saved by player number. Bots do not exist yet when the menu is open, so they are saved by slot (Bot 1, Bot 2). When the players or the bot count change, the saved plan is considered stale and the default split applies.
// Saved between sessions: H = human by PlayerId, B = bot by slot.
// Column: -1 red, 0 middle (not placed), 1 blue.
"H0:-1;H1:1;B0:0"
// The saved plan is only used if it still fits the players of this match.
public bool Matches(IReadOnlyCollection<int> humanIds, int botCount)
{
if (humanIds == null || botCount != _botTeams.Length || humanIds.Count != _humanTeams.Count)
return false;
return humanIds.All(_humanTeams.ContainsKey);
}As soon as Team Match is on, the match settings screen previews the teams: players already look like their team, but the rules (ghosting, friendly fire) are not applied yet. Visuals and gameplay read two different values, so the preview can never leak into a real match.
// Visuals read the "display" team: shown in a match AND in the settings preview.
public static TeamId? GetDisplayTeam(int playerId)
{
return AreTeamsShown ? _assignment.GetTeam(playerId) : null;
}
// Gameplay reads the "active" team: only during a real match.
public static TeamId? GetActiveTeam(int playerId)
{
return _isActive ? _assignment.GetTeam(playerId) : null;
}
// MatchSettingsScreen: the preview is on while the Team Match option is On.
if (preview) TeamMatch.SetPlan(TeamPlacementStore.LoadPlan(humanIds, botCount), humanIds, botCount);
TeamMatch.SetPreview(preview);Every attack goes through the same question: can this attacker hurt this target? Keeping it in one function means thrown objects, dashes and shots all follow the same rule.
// One rule, read everywhere: hits, projectiles, dashes and collisions.
public static bool IsFriendlyFireBlocked(int attackerId, int targetId)
{
return FriendlyFire == FriendlyFireMode.Off
&& AreTeammates(attackerId, targetId);
}
// In PawnHitHandler: a blocked hit simply does nothing.
if (TeamMatch.ShouldIgnoreHit(hitInfo.instigatorID, _pawn.GetOwner().PlayerId)) return;With friendly fire off, teammates also become ghosts to each other. Their colliders ignore one another, so nobody gets pushed, blocked or stood on by a teammate. With friendly fire on, they collide like opponents, so dashes can hit. The ignore pairs are rebuilt at every spawn, because a pawn can come back with new colliders.
// Friendly fire off: teammates' colliders ignore each other.
// Friendly fire on: they collide like opponents, so dashes can hit.
for (var i = 0; i < players.Count; i++)
{
for (var j = i + 1; j < players.Count; j++)
{
SetIgnored(players[i], players[j], TeamMatch.IsFriendlyFireBlocked(players[i], players[j]));
}
}
// Re-run on every spawn: a pawn can come back with colliders the pairs no longer cover.
if (TeamMatch.IsActive) TeamGhosting.Apply(PlayersManager.Instance.GetActivePlayerControllers());The point mini-games (jetpack race, basket, target master) add up the scores of each team. Tied teams all win, the same way tied players do.
// A team's score is the sum of its members' scores.
// Teams tied on the best total all win, like players tied on the best score.
public static List<int> BestTeamPlayers(
IReadOnlyCollection<KeyValuePair<int, int>> playerScores,
Func<int, TeamId> teamOf)
{
var totals = Totals(playerScores, teamOf);
if (totals.Count == 0) return new List<int>();
var best = totals.Values.Max();
return playerScores
.Where(pair => totals[teamOf(pair.Key)] == best)
.Select(pair => pair.Key)
.ToList();
}Survival rounds and duels used to end when one player was left. They now count sides: a round ends when only one team has players alive, and outside a team match a side is simply a player. This one change let every game mode work with teams without rewriting each one.
// Rounds used to end when one player was left. Now they count sides.
// Outside a team match a side is a player, so the old modes behave the same.
public static int CountSides(IReadOnlyCollection<PlayerController> players)
{
if (players == null) return 0;
if (!_isActive) return players.Count;
return _assignment.CountSides(players.Where(p => p).Select(p => p.PlayerId));
}
// GS_Duel: the duel is over when one side remains.
public bool IsGameFinished() => TeamMatch.CountSides(_winnersPlayers) <= 1;
// When one player wins, their whole team wins.
winners = TeamMatch.ExpandWinnersToTeams(winners, allPlayers);Team Match shipped alongside the studio's bot system, and the two had to work together from day one. Bots are first-class players in a team.

// BotPerception: in a team match, teammates are not opponents.
// No hunting, aiming or dashing at them.
if (TeamMatch.AreTeammates(selfOwner, controller)) continue;// Bots do not exist yet when the humans pick a side, so the default split
// counts the bots that will join at match start.
public void ApplyDefaultInOrder(IReadOnlyList<int> orderedPlayerIds, int totalCount = -1)
{
var total = Math.Max(totalCount, orderedPlayerIds.Count);
var redCount = (total + 1) / 2;
for (var i = 0; i < orderedPlayerIds.Count; i++)
{
var id = orderedPlayerIds[i];
if (IsValidPlayerId(id)) _teams[id] = i < redCount ? TeamId.Red : TeamId.Blue;
}
Changed?.Invoke();
}
// The match start applies the plan: humans by PlayerId, bots by creation order.
foreach (var id in humans) _assignment.SetTeam(id, _plan.HumanTeams[id]);
for (var i = 0; i < bots.Count; i++) _assignment.SetTeam(bots[i], _plan.BotTeams[i]);// Static state survives "Enter Play Mode without domain reload".
// Reset it explicitly so every session starts clean.
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStatics()
{
_assignment = CreateAssignment();
_isActive = false;
_isPreviewing = false;
_friendlyFire = FriendlyFireMode.On;
_plan = null;
ActiveChanged = null;
VisualsChanged = null;
}Royal Vermin is developed by Tobafeu Studio in Unity, with Rewired, VContainer and DOTween.