I have been trying to have my enemy react to the player and if there are any nearby enemies within its sphere collider trigger that those enemies will also be aggroed and target and chase the player. If there is a better way of handling this kind of behavior then please let me know since I am still learning how to develop games and using unity. Here is the script I have so far.
public class Enemy : MonoBehaviour {
public bool gamePaused = false;
public bool isAggroed = false;
public GameObject player;
public bool inSight = false;
// Update is called once per frame
void Update ()
{
if (isAggroed == true)
{
if (Vector3.Distance(player.transform.position, transform.position) > 3)
{
transform.LookAt(player.transform.position);
rigidbody.velocity = transform.forward * 5;
}
else
{
rigidbody.velocity = transform.forward * 0;
}
}
}
public void OnTriggerEnter(Collider collider)
{
//Debug.Log("Enter");
PathFollowing continuePath = transform.GetComponent<PathFollowing>();
if(collider.CompareTag("Player"))
{
continuePath.followOn = false;
//inSight = true;
isAggroed = true;
}
}
public void OnTriggerExit(Collider collider)
{
//Debug.Log("Exit");
PathFollowing continuePath = transform.GetComponent<PathFollowing>();
if (collider.CompareTag("Player"))
{
continuePath.followOn = true;
//inSight = false;
isAggroed = false;
}
}