I have a misbehave by guards that are patrolling, if attacked they stop walking and starting fight (1), but the other guard (2) still walking as nothing is happening, also the guard 2 won’t detect the enemy.
Also, when the first Enemy detect the the guard and start attacking, the second enemy one won’t start.
In AIController.cs I’m using the CheckForEnemy inside Update which is running if the enemy variable is null, and if it is null, still runs the Patrol (if it is present)…
private void Update()
{
if (health.IsDead()) return;
if (fighter.enabled)
{
if(enemy != null && fighter.enemy != null)
{
if (IsAggrovated(fighter.enemy.transform) && fighter.CanAttack(fighter.enemy.gameObject))
{
AttackBehaviour();
}
else if (timeSinceLastSawEnemy < suspicionTime)
{
SuspiciousBehaviour();
}
}
else
{
CheckForEnemy();
if(enemy == null)
PatrolBehaviour();
}
}
TimerUpdater();
}
private void CheckForEnemy()
{
RaycastHit[] hits = Physics.SphereCastAll(transform.position, chaseDistance, Vector3.up, 0);
// Loop over all hits
foreach (RaycastHit hit in hits)
{
if (hit.transform == transform) continue;
// Find any ally components
Fighter enemy = hit.transform.GetComponent<Fighter>();
// not an enemy at all
if (enemy == null) continue;
// the same enemy that we already have
if (this.enemy == enemy) this.enemy = null;
// It can be killed!
if (enemy.GetComponent<Health>() == null) continue;
// The enemy is dead
if (enemy.GetComponent<Health>().IsDead()) continue; // No need to update Fighter's target 'cause it will take care of it.
// The same faction never fight each-other
if (fighter.faction == enemy.faction) continue;
// We are friendly =)
if (gameMaster.GetReputation(fighter.faction, enemy.faction) > 50f) continue;
// Our Fighter component is disabled for some reason, maybe it's a pray or a child \_("/)_/
if (!fighter.enabled && !health.IsDead())
mover.RunAway(enemy.transform);
this.enemy = enemy.gameObject;
// This is the method that is placed in Health's Event TakeDamage
Alerted();
mover.Cancel();
if (IsAggrovated(enemy.transform) && fighter.CanAttack(enemy.gameObject))
{
AttackBehaviour();
}
else if (timeSinceLastSawEnemy < suspicionTime)
{
SuspiciousBehaviour();
}
// We have an enemy so, stops cycling...
return;
}
}
private void AttackBehaviour()
{
timeSinceLastSawEnemy = 0;
fighter.Attack(enemy);
AlertAllies();
}
public void Alerted()
{
timeSinceAllerted = 0;
}
private void AlertAllies()
{
RaycastHit[] hits = Physics.SphereCastAll(transform.position, shoutDistance, Vector3.up, 0);
// Loop over all hits
foreach (RaycastHit hit in hits)
{
// Find any ally components
AIController ai = hit.transform.GetComponent<AIController>();
if (ai == null) continue;
// Not a fighter, no sense to alert
Fighter fighter = hit.transform.GetComponent<Fighter>();
if (fighter == null) continue;
// Not an Ally, will not support us!
if (gameMaster.GetReputation(fighter.faction, ai.fighter.faction) < 55f) continue;
// Aggro-vate ally
ai.Alerted();
}
}
After a bit of tests I notice that, while the guard is walking didn’t react to the enemies
Any advice?