Here is a script called UnitGroupPlacer.cs that demonstrates how to move a group of soldiers, led by a commander, to a target position using Unity NavMesh Agents. You will need to have a NavMesh baked in your scene for the agents to navigate.
First, you will need to set up your Unity project by adding a NavMesh to your scene and making sure your characters have NavMeshAgent components attached.
Next, create the UnitGroupPlacer.cs script and attach it to an empty game object in your scene. Then, assign the commander and the soldiers in the inspector.
using UnityEngine;
using UnityEngine.AI;
using System.Collections.Generic;
public class UnitGroupPlacer : MonoBehaviour
{
[SerializeField] private NavMeshAgent commanderAgent;
[SerializeField] private List<NavMeshAgent> soldiersAgents;
[SerializeField] private int numberOfRows;
private float soldierSpacing = 2.0f; // Adjust this value to control the space between soldiers
private void Awake()
{
if (commanderAgent == null || soldiersAgents == null)
{
Debug.LogError("Commander and/or soldiers are not assigned.");
}
if (numberOfRows <= 0)
{
Debug.LogError("Number of rows should be a positive integer.");
}
}
public void MoveGroup(Vector3 targetPosition)
{
// Move the commander to the target position
if (commanderAgent.isActiveAndEnabled)
{
commanderAgent.SetDestination(targetPosition);
}
// Calculate positions for the soldiers based on the commander's position
int soldiersPerRow = soldiersAgents.Count / numberOfRows;
Vector3 rowStartPoint = targetPosition - (Vector3.right * ((soldiersPerRow - 1) * soldierSpacing / 2));
int currentSoldierIndex = 0;
for (int row = 0; row < numberOfRows; row++)
{
for (int i = 0; i < soldiersPerRow && currentSoldierIndex < soldiersAgents.Count; ++i)
{
NavMeshAgent soldierAgent = soldiersAgents[currentSoldierIndex];
Vector3 soldierPosition = rowStartPoint + Vector3.right * i * soldierSpacing + Vector3.back * row * soldierSpacing;
if (soldierAgent.isActiveAndEnabled)
{
soldierAgent.SetDestination(soldierPosition);
}
currentSoldierIndex++;
}
rowStartPoint -= Vector3.back * soldierSpacing;
}
}
// Call this method when you want to move the group
public void MoveToTarget(Vector3 targetPos)
{
MoveGroup(targetPos);
}
// You can also make the group move from the editor by calling this method
// through a custom editor script or by adding a context menu
[ContextMenu("MoveToRandomTarget")]
private void MoveToRandomTarget()
{
Vector3 randomTarget = new Vector3(Random.Range(-10f, 10f), 0, Random.Range(-10f, 10f));
MoveGroup(randomTarget);
}
}
This script has serialized fields for setting up the commanderAgent (the NavMeshAgent for the commander), soldiersAgents (the list of NavMeshAgents for soldiers), and numberOfRows (to determine the structure of the group).
To execute movement, call the MoveToTarget(Vector3 targetPos) method from another script or bind it to a UI button click event, input action, etc. Ensure that you have attached an instance of a NavMeshAgent Component to both the commander and each soldier for the script to work.
Finally, you can improve the placements by considering the agent's radius and avoiding overcrowded rows. Make sure to adjust soldierSpacing accordingly to create a coherent group formation. If you're going to execute MoveGroup() at runtime from another script, remember to make it public or use an event system to trigger it.