I am creating a top down 2D game that uses a AI system so my enemies (skeletons) follow the player. I already have the code set up to follow the player, and it does so successfully, but the AI ignores all collision except for the player. The skeletons have a "IsTrigger" on their circle colliders, because my code to hurt the player upon trigger enter requires that so they don't move the player around. But if I have multiple skeletons at the same time, they will eventually move inside of each other. I have tried putting a box collider inside of the circle collider.
AI Code:
public GameObject EnemyDeathParticle;
public LevelManager levelManager;
public Transform skeleton;
public Transform target;//set target from inspector instead of looking in Update
public float speed = 3f;
void Start () {
}
void Update(){
//rotate to look at the player
transform.eulerAngles = new Vector3 (0, 0, -transform.eulerAngles.z);
transform.LookAt(target.position);
transform.Rotate(new Vector3(0,-90,0),Space.Self);//correcting the original rotation
//move towards the player
if (Vector3.Distance (transform.position, target.position) > 0.4f) {//move if distance from target is greater than 1
transform.Translate (new Vector3 (speed * Time.deltaTime, 0, 0));
}
}
}
Skeleton Attack Code:
public float timeBetweenAttacks = 0.5f; // The time in seconds between each attack.
public int attackDamage = 10; // The amount of health taken away per attack.
Animator anim; // Reference to the animator component.
GameObject player; // Reference to the player GameObject.
GameObject skeleton;
PlayerHealth playerHealth; // Reference to the player's health.
AI enemyHealth; // Reference to this enemy's health.
bool playerInRange; // Whether player is within the trigger collider and can be attacked.
float timer; // Timer for counting up to the next attack.
void Awake ()
{
// Setting up the references.
player = GameObject.FindGameObjectWithTag ("Player");
skeleton = GameObject.FindGameObjectWithTag ("Skeleton");
playerHealth = player.GetComponent ();
enemyHealth = skeleton.GetComponent();
anim = GetComponent ();
}
void OnTriggerEnter2D (Collider2D other)
{
// If the entering collider is the player...
if(other.gameObject == player)
{
// ... the player is in range.
playerInRange = true;
}
if (other.gameObject == skeleton) {
}
}
void OnTriggerExit2D (Collider2D other)
{
// If the exiting collider is the player...
if(other.gameObject == player)
{
// ... the player is no longer in range.
playerInRange = false;
}
if (other.gameObject == skeleton) {
}
}
void Update ()
{
// Add the time since Update was last called to the timer.
timer += Time.deltaTime;
// If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
if(timer >= timeBetweenAttacks && playerInRange) //&& enemyHealth.currentHealth > 0
{
// ... attack.
Attack ();
}
// If the player has zero or less health...
if(playerHealth.CurrentHealth <= 0)
{
// ... tell the animator the player is dead.
anim.SetTrigger ("PlayerDeath");
}
}
void Attack ()
{
// Reset the timer.
timer = 0f;
// If the player has health to lose...
if(playerHealth.CurrentHealth > 0)
{
// ... damage the player.
playerHealth.HurtPlayer (attackDamage);
}
}
}
↧