Ahh, this one is not a question, rather it’s a short documentary. If you’ve been secretly spying on my posts for now, you may have noticed that I had a serious problem with my AggroGroup recently, and it’s the fact that for god knows what reason, it wasn’t working properly for a while. After a few hours of extreme investigation, I figured out why. Here’s what made it work for me extremely well:
- Ensure your ‘Max Nav Path Length’ in your ‘Mover.cs’ script is set to a high enough value, which will allow your guards to actually be able to catch up with you (if the guy is too far, he won’t be able to reach you. At that point, he’ll just ignore you. If your enemies will be called from a very far distance from you, you’ll absolutely want to crank this one up!)
- Inject this block of code in your ‘AggroGroup.Activate()’ method, under the ‘if’ statement that gets your fighters’ CombatTarget component (AFTER THE ‘if’ BLOCK, NOT INSIDE IT). Don’t forget to delete the debuggers, unless you want to improve on this code:
if (fighter.TryGetComponent(out NavMeshAgent navMeshAgent))
{
navMeshAgent.enabled = shouldActivate;
if (shouldActivate)
{
Transform playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
Debug.Log($"Setting destination to: {playerTransform.position}");
navMeshAgent.SetDestination(playerTransform.position);
Debug.Log($"{navMeshAgent.SetDestination(playerTransform.position)}");
navMeshAgent.isStopped = false;
fighter.GetComponent<Mover>().MoveTo(playerTransform.position, 1.0f);
Debug.Log("Fighter coming at you");
}
}
- Inject a single line into your ‘AggroGroup.RemoveFighterFromGroup()’ and ‘void Start()’ (PLACE IT AT THE TOP OF YOUR Start() function, else it won’t work!) function:
// Remove null or destroyed fighters from the list
fighters.RemoveAll(fighter => fighter == null || fighter.IsDestroyed());
This ensures you have no null fighters in your group. If your group is initially crowded with empty fighters, your aggroGroup simply won’t work!
- Put this on top of your foreach loop in ‘AggroGroup.Activate()’:
if (fighter == null) {
Debug.Log("Null fighter found");
return;
}
It’s a safety check to ensure you don’t accidentally throw Null fighters into your AggroGroup (again, another safety check to ensure the group works properly).