I’m not sure if this is the best method, but I don’t really know how to properly implement this function. I have an enemy with two sphere colliders. One is on the parent and the other is on the child. The parent sphere is responsible for the line-of-sight, if the player enter’s his line of sight, then he will attack and chase the player. The child sphere is responsible for alerting other enemies if their child sphere’s are colliding. So here’ part of my code to do it:
//script for parent
public void OnTriggerStay(Collider playerEntered)
{
isAlerted = false;
float distance = Vector3.Distance(player.transform.position, transform.position);
//player enters enemy's line of sight
if(playerEntered.gameObject.tag == "Player")
{
isAlerted = true;
//attack the player
if(isAlerted == true)
{
if(distance > attackDistance)
{
Quaternion rotation = Quaternion.LookRotation(player.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
renderer.material.color = Color.red;
}
}
}
else
{
isAlerted = false;
}
}
//script for child sphere collider
public void OnTriggerStay(Collider enemyAlert)
{
//if player is in sight, alert nearby zombie
if(enemyAIScript.isAlerted == true enemyAlert.gameObject.tag == "Enemy")
{
//attack enemy
//what to put here?
}
}