- Home /
How to make Ui based on how many Ai agents are attacking / following ?
Hello I want to make an system that allows the player of the game to know if he is currently chased or being attacked by an Ai client. Easy so far but the problem is that multiple agents will be attacking and ending the attack at the same time. If I fore example have an int of _isChased at the player script, and make it +1 every time an ai is chasing, I need to lower it when the ai is not chasing anymore. But when I am in idle, I dont know which previous values have been set by an agent. and the true value could range somewhere from -4 to whatever when it should be at 0. Same with Event manager, too many methods.
Answer by winterfluxstudio · May 19, 2018 at 07:43 PM
I don't think you can get away with something simple. To achieve that would be a somewhat complex arrangement due to the amount of variables that system might have to deal with.
imo....
1) a central manager script that handles calculating (and displaying) this information 2) enemies have an additional script component specifically for interacting with this manager script
ChaseManager.cs
// what is the lowest possible amount of enemies chasing the player
public int chasedByMinimum;
// what is the maximum, if any?
public int chasedByMaximum;
// is the player being chased? (doesnt matter how many enemies are chasing them)
public bool playerChased;
// how many enemies are chasing the player?
public int chaseTotal;
EnemyChase.cs
// reference the ChaseManager GameObject
public GameObject ChaseManagerGO;
// reference the ChaseManager.cs script
private ChaseManager chaseManager;
void Start()
{
// reference the ChaseManager script
chaseManager = ChaseManagerGO.GetComponent<ChaseManager>();
}
The way to resolve the issue you pointed out is to handle the addition and subtraction in the script attached to an enemy.
The enemy script would contain the data that allows you to figure out if it's chasing the player. So, you might have
ChasePlayer()
{
// enable running animation
chaseManager.chaseTotal += 1;
}
StopChasingPlayer()
{
// Disable running animation
// Switch to idle animation
chaseManager.chaseTotal -= 1;
}
The methods used to add/subtract should only be called once. the method that handles updating the enemy path should be handled in a separate method. Then it will only increment/decrement by 1 per enemy (regardless of how many enemies are in the scene).
Answer by mcmount · May 19, 2018 at 07:18 PM
Why don't you just use tags for the agents to identify the status. When agent chases, change his tag "idling" to "chasing". Then you just need to check every few seconds if any agent has the tag "chasing". Then just count how many "chasing" tags there are. gameObject.tag = "idling" OR gameObject.tag = "chasing"
Your answer
