- Home /
"Unique" enemy problem (health, animations, etc)
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
Answer by siaran · May 27, 2014 at 01:07 PM
Looks like your EnemyAI scripts are getting the playerInRange and destroyBullet.enemyHit variables from the same object.
Seems like you have a variable playerInRange on your PlayerMovement script - and then you check the value of that in the enemy script. You should determine if your player is in range on the enemy script, because now, when you set playerInRange to true on your player ALL enemies will have PlayerMovement.playerInRange as true (this is why both enemies begin attacking even when you only step in one trigger).
Similar problem with your bullet: it looks like you have every enemy script asking a bullet if it's hit anything, then have all of them take damage when it has. You should make a method in your bullet script that determines what enemy it's hit and then call a TakeDamage method on that enemy's script.
Thanks again - I think my disconnect was because I was trying to access a trigger that I had parented my enemy characters hip bone (so the collider moved with the player, not the root)
I've ready that you shouldn't use "static" variables.. Is this true?
Your answer
Follow this Question
Related Questions
iPhone Unique Identifier 1 Answer
Enemy Database for RPG Battle System? 1 Answer
When I kill one of the two enemies, both dies 3 Answers
How can I add a value to one part of a raycast? 0 Answers
Why don't my enemies walk toward me? 1 Answer