- Home /
Enemy Attraction/Aggro
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;
}
}
do u have an issue atm? depending on how simple you want your AI this seems to be a pretty good start so far
Answer by TypeFinalMagic · Jul 13, 2013 at 01:18 AM
For the most part it is working if the player capsule is in the enemy sphere colliders they chase the player but if the player goes into two enemy colliders and leaves enemy2, enemy2 will go back to following on the path but since the player is in the collider of enemy1 and enemy2 is still in the collider of enemy1 then enemy2 should still be aggroed and chase the player.
Your answer
Follow this Question
Related Questions
Enemy AI With changing Player 0 Answers
How do you move did with [iTWEEN] the enemy has a component of [Character Controller]? 0 Answers
Funny error. collision just hit 12 times 1 Answer
Need help with enemy AI 1 Answer
AI Enemy script 1 Answer