Hello,
Apologies, I'm still struggling... Whilst I can move the units in formation a few times and change their leader, I will eventually get an Argument Out Of Range Exception: Index was out of range. Parameter name:index error when changing their position. Would you be able to sense check the below to ensure I have the fundamentals correct?
I have three agents in my game, I can select multiple agents at once and add them into a list. When I select an agent/s, one agent gameObject will be assigned to a Leader variable in the ActorManager script.
Each of my agents has a behavior tree like this:
And an Agent script like this:
behavior = GetComponent<Behavior>();
actorManager = GetComponentInParent<ActorManager>();
public void SetDestination(Vector3 destination)
{
if(behavior)
{
if (actorManager.Leader == this.gameObject)
{
behavior.SetVariableValue("Leader", null);
behavior.SetVariableValue("Direction", destination);
}
else
behavior.SetVariableValue("Leader", actorManager.Leader);
}
else
agent.destination = destination;
}
When I select my agent, the Leader value is correctly assigned by the ActorManager script (here's the code snippet):
public GameObject Leader;
Leader = null;
foreach (RaycastHit hit in hits)
if (hit.collider.TryGetComponent(out Actor actor))
{
if (Leader == null)
Leader = actor.gameObject;
else
{
actor.behavior.SetVariableValue("Leader", Leader);
}
selectedActors.Add(actor);
actor.visualHandler.Select();
}
Any feedback is appreciated.
Apologies, I'm still struggling... Whilst I can move the units in formation a few times and change their leader, I will eventually get an Argument Out Of Range Exception: Index was out of range. Parameter name:index error when changing their position. Would you be able to sense check the below to ensure I have the fundamentals correct?
I have three agents in my game, I can select multiple agents at once and add them into a list. When I select an agent/s, one agent gameObject will be assigned to a Leader variable in the ActorManager script.
Each of my agents has a behavior tree like this:
And an Agent script like this:
behavior = GetComponent<Behavior>();
actorManager = GetComponentInParent<ActorManager>();
public void SetDestination(Vector3 destination)
{
if(behavior)
{
if (actorManager.Leader == this.gameObject)
{
behavior.SetVariableValue("Leader", null);
behavior.SetVariableValue("Direction", destination);
}
else
behavior.SetVariableValue("Leader", actorManager.Leader);
}
else
agent.destination = destination;
}
When I select my agent, the Leader value is correctly assigned by the ActorManager script (here's the code snippet):
public GameObject Leader;
Leader = null;
foreach (RaycastHit hit in hits)
if (hit.collider.TryGetComponent(out Actor actor))
{
if (Leader == null)
Leader = actor.gameObject;
else
{
actor.behavior.SetVariableValue("Leader", Leader);
}
selectedActors.Add(actor);
actor.visualHandler.Select();
}
Any feedback is appreciated.