Formation Pack With Army Path

pegasusejder

New member
First of all, I had a problem with the formation package, I just bought the formation package and I am having difficulty using it. I added a behavior tree to the agents and assigned a grid formation. I selected the leader and target point. When I start the game, it gives me the error I got and they don't move. When I set the leader object to none, they move to the target, but this time they all move alone, without following the formation. How can I solve this?
1718086803771.png
1718086822384.png

In the next step, I want to do this, for now, I draw a path (the red line in the picture) to an object and then the object follows this path (I wrote the movement system myself). I want to integrate this into the army, that is, I will draw a path for an army and the soldiers and the army will follow that path in the desired formation. How can I do that?

1718086977957.png
 
It looks like the agent is being removed from the group, what is causing it to leave the group? Is a task ending?

What does your leader tree look like? Make sure you do not specify a leader for the leader object.

In the next step, I want to do this, for now, I draw a path (the red line in the picture) to an object and then the object follows this path (I wrote the movement system myself). I want to integrate this into the army, that is, I will draw a path for an army and the soldiers and the army will follow that path in the desired formation. How can I do that?
On your leader tree you'll need to create a new task which has the leader follow that path instead of the built in tasks. The agents can then follow the leader's movements.
 
Thanks for the answer!
It looks like the agent is being removed from the group, what is causing it to leave the group? Is a task ending?

What does your leader tree look like? Make sure you do not specify a leader for the leader object.
Oh, I understand a little better now. Yes, I had appointed the leader himself as the leader, or rather, since I gave the same reference to all of them, they all had the same leader (including the leader). I didn't realize this was causing trouble.

It looks like the agent is being removed from the group, what is causing it to leave the group? Is a task ending?

What does your leader tree look like? Make sure you do not specify a leader for the leader object.


On your leader tree you'll need to create a new task which has the leader follow that path instead of the built in tasks. The agents can then follow the leader's movements.
At the same time, I was giving a target point to all of them, but it was enough to give it only to the leader, I understood this from what you said.

I need to constantly change the target transform of the leader in the code.

Well let me ask you this, this will be a war game and the armies will fight each other. How can I determine a new leader in a situation where the leader dies?


1718097128216.png

I also want to ask this, I have one main object and all the agents are in it. There is a path drawing algorithm in this main object, and I start the path by sending a ray at the main object's collider (triggering the collider of the main object). In a situation where the agents themselves have a movement system, I cannot move both the main object and the agents. How do you think I can do this?

The method that came to my mind was this, the leader will follow the main object and the main object will follow the path.
 
Last edited:
Well let me ask you this, this will be a war game and the armies will fight each other. How can I determine a new leader in a situation where the leader dies?
You can register for the StartListeningForOrders/StopListeningForOrders events. In the demo scene there is a section called Dynamic Groups and that shows a good example of switching leaders.

I also want to ask this, I have one main object and all the agents are in it. There is a path drawing algorithm in this main object, and I start the path by sending a ray at the main object's collider (triggering the collider of the main object). In a situation where the agents themselves have a movement system, I cannot move both the main object and the agents. How do you think I can do this?
I think that your best solution will be to create a new task that the leader follows. This will give you the most flexibility in terms of following that main object. It can actually be a pretty simple task in that you just need to have the agent set the destination of the main object.
 
You can register for the StartListeningForOrders/StopListeningForOrders events. In the demo scene there is a section called Dynamic Groups and that shows a good example of switching leaders.


I think that your best solution will be to create a new task that the leader follows. This will give you the most flexibility in terms of following that main object. It can actually be a pretty simple task in that you just need to have the agent set the destination of the main object.
Thank you, I will review what you said, if I get stuck on anything, I'll be here with you again. ☺️
 
@Justin hello again, I'm having a little problem.

I set the formation and the object that the formation will follow, everything worked normally until here. Then I added 3 more trees to the agents and each one has a different formation (Grid, Wedge, Diamond and Triangle). Whenever I try to change the formation, the Leader agent follows the target, but the remaining agents stay where they are. Then, when starting the game, I started the game with the Wedge formation, this time the leader follows the target but the other agents remain motionless. When I move the Wedge tree to the top (above the Grid tree) in the Inspector window, the Wedge formation works. So, only the top tree in the Inspector window works properly, the rest remain motionless even if the leader moves.

Below is the code I wrote and sample images, I can send you anything else if needed.
After the formation changes, values are assigned to the Target and Leader variables, I checked them.

C#:
public class SetFormation : MonoBehaviour
{
    [SerializeField] List<AgentFormation> m_Agents;
    FormationTypes m_FormationType;
    [SerializeField] Transform target;
    private void Awake()
    {
        SetFormationToAgents(1);
    }

    public void SetFormationToAgents(int index)
    {
        foreach(AgentFormation agent in m_Agents)
        {
            StopCoroutine(agent.EnableBehavior(index));
            if(agent != m_Agents[0])
            {
                agent.SetLeader(m_Agents[0].gameObject, index);
                
            }
            else
            {
                agent.SetTarget(target, index);
            }
            StartCoroutine(agent.EnableBehavior(index));
        }
    }

    public void RemoveAgentList(AgentFormation agent)
    {
        if (agent == m_Agents[0] && m_Agents.Count > 1)
        {
            SetLeaderToAgents(m_Agents[1]);
            m_Agents.RemoveAt(0);
        }
        else m_Agents.Remove(agent);
    }

    public void SetLeaderToAgents(AgentFormation agent)
    {
        foreach (var _agent in m_Agents)
        {
            if (agent != _agent) _agent.SetLeader(m_Agents[0].gameObject);
            else _agent.SetTarget(target);
        }
    }
}

C#:
public class AgentFormation : MonoBehaviour
{
    [SerializeField] List<BehaviorTree> behaviorTrees;
    BehaviorTree currentBehavior;
    public GameObject leader;
    [SerializeField] SetFormation setFormation;


    private void OnDisable()
    {
        setFormation.RemoveAgentList(this);
    }

    public IEnumerator EnableBehavior(int index)
    {
        if(currentBehavior != null)currentBehavior.DisableBehavior();
        currentBehavior = behaviorTrees[index];
        currentBehavior.EnableBehavior();
        yield return null;
    }

    public void SetLeader(GameObject obj)
    {
        leader = obj;
        var value = (SharedGameObject)currentBehavior.GetVariable("Leader");
        value.Value = leader;
        currentBehavior.SetVariableValue("Leader", obj);
    }

    public void SetLeader(GameObject obj, int index)
    {
        leader = obj;
        var value = (SharedGameObject)behaviorTrees[index].GetVariable("Leader");
        value.Value = leader;
        behaviorTrees[index].SetVariableValue("Leader", obj);
    }

    public void SetTarget(Transform target)
    {
        var value = (SharedTransform)currentBehavior.GetVariable("Target");
        value.Value = target;
        currentBehavior.SetVariableValue("Target", target);
    }
    public void SetTarget(Transform target, int index)
    {
        var value = (SharedTransform)behaviorTrees[index].GetVariable("Target");
        value.Value = target;
        behaviorTrees[index].SetVariableValue("Target", target);
    }
}


1719048918724.png

1719049090454.png
 
Last edited:
It's tough to say without stepping through the code, but take a look at BehaviorDesigner.Runtime.Formations.BehaviorSelection.EnableBehavior for an example of switching leaders. Make sure when switching leaders that the new leader's Leader value is null. In addition, only one Formation Pack task should be running at a time on a single GameObject so when you have three behavior trees added to your GameObject you should only have one active at a time.
 
It's tough to say without stepping through the code, but take a look at BehaviorDesigner.Runtime.Formations.BehaviorSelection.EnableBehavior for an example of switching leaders. Make sure when switching leaders that the new leader's Leader value is null. In addition, only one Formation Pack task should be running at a time on a single GameObject so when you have three behavior trees added to your GameObject you should only have one active at a time.
I solved the problem, I had a problem because I did not set the Leader Group Index value. When I corrected those values, the problem was resolved.
 
Back
Top