I have an enemy prefab, with an enemyAI script attached. The prefab is a rigged and animated character (using animator)
My enemy has 5 hp. I drag two prefabs into the scene and play - If I shoot the first enemy, which is closest to me, both enemies play the “hit” animation. Also, both enemies die at the same time, even though I’m only attacking the closest.
If I step into the first enemies trigger, both enemies begin attacking, even though the second enemy is far away.
This is my enemyAI script
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
private Animator anim;
private Transform stinger;
public GameObject ParticlePrefab;
public GameObject health;
public GameObject instantiated;
public Rigidbody stingerPrefab;
Rigidbody clone;
private float bulletSpeed = 1500.0f;
public bool step = true;
public bool playerInRange = false;
public int enemyHealth = 5;
void Start ()
{
stinger = GameObject.Find("stingerPos").transform;
anim = GetComponentInChildren<Animator>();
}
void Update ()
{
playerInRange = PlayerMovement.playerInRange;
anim.SetBool ("Detected", playerInRange);
anim.SetBool ("enemyHit", destroyBullet.enemyHit);
if(destroyBullet.enemyHit)
--enemyHealth;
if(playerInRange && step)
Attack();
if(enemyHealth <= 0)
{
Destroy (gameObject);
instantiated = (GameObject)Instantiate(ParticlePrefab, transform.position, transform.localRotation);
if(PlayerMovement.playerHealth < 3)
instantiated = (GameObject)Instantiate(health, transform.position, transform.localRotation);
}
}
IEnumerator AttackWait()
{
step = false;
yield return new WaitForSeconds(0.75f);
step = true;
}
void Attack()
{
int attackProb = Random.Range(0,7);
if(attackProb == 3 || attackProb == 1)
{
if(!destroyBullet.enemyHit)
{
clone = Instantiate(stingerPrefab, stinger.position, stinger.localRotation) as Rigidbody;
clone.AddForce(transform.forward * bulletSpeed);
StartCoroutine(AttackWait());
}
}
else
{
StartCoroutine(AttackWait());
}
}
}
How can I make enemies unique? I would like to have many enemies in the scene, not just 2
Any ideas on what I’m doing wrong? Thanks