Hi, so I'm currently trying to set my enemies aggro bool to true if another enemy takes damage.
//list for enemies
public List<CharacterController> enemies = new List<CharacterController>();
//variables for aggro range and aggressive check
public float aggroRange;
public bool isAggressive;
int nearestTarget = 0;
//cycling through player character list
for (int i = 1; i < GameManager.instance.playerCharacters.Count; i++)
{
//if any of the player characters positions arent within our aggro range
if (Vector3.Distance(transform.position, GameManager.instance.playerCharacters[nearestTarget].transform.position) > aggroRange)
{
//they are not aggressive
isAggressive = false;
// if non aggressive enemies health becomes lower than max health, they have taken damage
if (GameManager.instance.activePlayer.currentHealth < GameManager.instance.activePlayer.maxHealth)
{
//|||| Figure out how to cylce through enemies and set them all to aggressive when one takes damage ||||
foreach (CharacterController allies in enemies)
{
//solution 1 - setting larget aggro range so they aggro anyway
//aggroRange = 20f;
//isAggressive = true;
//solution 2 - accessing the component on the script?
//allies.GetComponent<AIBrain>().aggroRange = 20f;
//allies.GetComponent<AIBrain>().isAggressive = true;
}
//set them as aggressive - Disables when fixed above logic
isAggressive = true;
}
}
//players are within AI aggro range
else
{
//set them as aggressive
isAggressive = true;
}
`敌人被正确添加到列表中,并试图在第二个 foreach 循环中循环遍历列表并设置它们具有侵略性。尝试过的解决方案在循环中被评论无济于事。
如果格式不好,我深表歉意,我是堆栈溢出的新手。`
使用事件通常是一种更有效和更有组织的方式来处理这种情况😁
实现此目的的一种方法是向您的 AIBrain 脚本添加一个事件,该事件将在该角色受到伤害时触发。然后,您可以订阅该事件,当事件被触发时,将所有订阅角色的 isAggressive 布尔值设置为 true。
在你的 AIBrain 脚本中,定义一个受到伤害的事件:
公共事件 Action OnTakeDamage;
public void TakeDamage()
{
// take damage logic...
// trigger the OnTakeDamage event
OnTakeDamage?.Invoke(this);
}
当敌人受到伤害时,你需要调用 TakeDamage() (所以无论你在哪里受到伤害,也调用它)。在您的 AIBrain 脚本中,为敌人列表中的每个敌人订阅 OnTakeDamage 事件:
void Start()
{
// subscribe to OnTakeDamage event for each enemy in the enemies list
foreach (AIBrain enemy in enemies)
{
enemy.OnTakeDamage += OnEnemyTakeDamage;
}
}
void OnDestroy()
{
// unsubscribe from OnTakeDamage event for each enemy in the enemies list
foreach (AIBrain enemy in enemies)
{
enemy.OnTakeDamage -= OnEnemyTakeDamage;
}
}
void OnEnemyTakeDamage(AIBrain enemy)
{
// set isAggressive to true for all enemies
foreach (AIBrain ally in enemies)
{
ally.isAggressive = true;
}
}
通过此实现,每当敌人受到伤害并触发 OnTakeDamage 事件时,将为所有订阅的敌人调用 OnEnemyTakeDamage 方法,将其 isAggressive 布尔值设置为 true。
让我知道这是否有效,或者您是否需要任何帮助!
for (int i = 1; i < GameManager.instance.playerCharacters.Count; i++)
{
//if any of the player characters positions arent within our aggro range
if (Vector3.Distance(transform.position, GameManager.instance.playerCharacters[nearestTarget].transform.position) > aggroRange)
{
//they are not aggressive
//isAggressive = false;
// if non aggressive enemies health becomes lower than max health, they have taken damage
if (GameManager.instance.activePlayer.currentHealth < GameManager.instance.activePlayer.maxHealth)
{
//|||| Figure out how to cylce through enemies and set them all to aggressive when one takes damage ||||
foreach (CharacterController allies in enemies)
{
//solution 1 - setting larget aggro range so they aggro anyway
//aggroRange = 20f;
//isAggressive = true;
//solution 2 - accessing the component on the script?
allies.GetComponent<AIBrain>().aggroRange = 20f;
allies.GetComponent<AIBrain>().isAggressive = true;
}
//set them as aggressive - Disables when fixed above logic
//isAggressive = true;
isAggressive = false;
}
在循环解决问题后将 isAggressive 移动到。不确定为什么。