OK so since the transition to third person, I think my AggroGroup broke. What currently happens is if I assign any of my enemies to the AggroGroup, they won’t patrol, and… even if they’re nearby and I get into a fight with a member of the group, they won’t fight the player to try and defend their buddies. How do I fix this? As of last update, about 4 months ago, this was what my AggroGroup system looked like (I haven’t tested triggering the aggroGroup through dialogue as of yet, but for the moment I just want the basics to work):
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using RPG.Movement;
using Unity.VisualScripting;
using RPG.Attributes;
namespace RPG.Combat {
public class AggroGroup : MonoBehaviour
{
[SerializeField] List<Fighter> fighters = new List<Fighter>(); // fighters to aggregate when our player takes a wrong dialogue turn (and pisses everyone off)
[SerializeField] bool activateOnStart = false;
[SerializeField] bool resetIfPlayerDies = true; // this is left as a boolean because it allows us to choose which aggroGroup members reset their settings on players' death, and which groups may not reset themselves on death (depending on your games' difficulty)
[SerializeField] internal bool hasWeaponVisibleBeforeFirstTimeAggregation;
private bool isActivated; // Activates/deactivates the 'aggroGroup'
private bool playerActivated; // this boolean ensures that the group does not give up on trying to fight the player, if the main character dies, ensuring that they still fight him, even without the leader
private Health playerHealth;
private void Start() {
// Ensures guards are not active to fight you, if you didn't trigger them:
Activate(activateOnStart, hasWeaponVisibleBeforeFirstTimeAggregation);
}
private void OnEnable() {
if (resetIfPlayerDies) {
playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<Health>();
if (playerHealth != null) playerHealth.onDie.AddListener(ResetGroupMembers);
}
}
public void OnDisable() {
if (resetIfPlayerDies) {
Health playerHealth = GameObject.FindGameObjectWithTag("Player").GetComponent<Health>();
if (playerHealth != null) playerHealth.onDie.RemoveListener(ResetGroupMembers);
}
}
public void ResetGroupMembers() {
Activate(false, hasWeaponVisibleBeforeFirstTimeAggregation);
}
/* public void HandleAttack(Fighter fighter) {
if (!isActivated) {
Activate(true);
fighters.Remove(fighter);
}
} */
public void Activate(bool shouldActivate, bool hasWeaponRendered = false)
{
// First step is to clean up the list, because if you don't, other fighters won't get involved in the fight:
fighters.RemoveAll(fighter => fighter == null || fighter.IsDestroyed());
isActivated = shouldActivate;
foreach (Fighter fighter in fighters)
{
if (fighter == null) RemoveFighterFromGroup(fighter);
else {
if (hasWeaponRendered) {
WeaponConfig enemyWeaponConfig = fighter.currentWeaponConfig;
fighter.AttachWeapon(enemyWeaponConfig);
}
}
// If you don't have a fighter script, stay out of this fight:
if (!fighter) continue;
fighter.enabled = shouldActivate;
if (fighter.TryGetComponent(out CombatTarget target)) target.enabled = shouldActivate;
// ----------------------------- TEST FUNCTION (Delete if failed): Setting destination of enemies to rush to attack the player: ----------------------
if (fighter.TryGetComponent(out NavMeshAgent navMeshAgent))
{
navMeshAgent.enabled = shouldActivate;
if (shouldActivate)
{
Transform playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
navMeshAgent.SetDestination(playerTransform.position);
navMeshAgent.isStopped = false;
fighter.GetComponent<Mover>().MoveTo(playerTransform.position, 1.0f);
}
}
// --------------------------- END OF TEST FUNCTION --------------------------------------------------------------------------------------------------
}
}
public void AddFighterToGroup(Fighter fighter) {
// If you got the fighter you want to add on your list, return:
if (fighters.Contains(fighter)) return;
// For other fighters on the list, add them lah:
fighters.Add(fighter);
fighter.enabled = isActivated;
if (fighter.TryGetComponent(out CombatTarget target)) target.enabled = isActivated;
}
public void RemoveFighterFromGroup(Fighter fighter) {
// Remove fighters from the list
fighters.Remove(fighter);
}
}
}
Another day, another fallen major system that needs addressing… (or at least are we addressing this later on?). If it helps, I also know the AggroGroup is somehow connected to my ‘RespawnManager.cs’ and ‘DialogueAggro.cs’. Should I provide these two scripts as well?