- Home /
Create number of enemies when one is deid
Hello everyone...
Her is the thing. Im doing a game for fun... so far i manged to do the Terrain, FPS Controller, Now im in the enemy part of the game..
So far i managed to make the character animate when its dead and it follows the user.
Now the question i have .....
How i can make the script creates a random enemies when all of them die...????
Any recommended improvements????? Thanks..
Enemy Script/....
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;
public var Health = 100;
function Start ()
{
}
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist)
{
transform.position = transform.position + transform.forward*MoveSpeed*Time.deltaTime;
if(Health >=1){
animation.Play("walk");
}else if (Health <= 0){
animation.CrossFade("die");
Destroy(gameObject, 2);
}
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
if(Health >=1){
animation.CrossFade("attack");
}else if (Health <= 0){
animation.CrossFade("diehard");
Destroy(gameObject, 2);
}
}
}
}
public function applyDamage(health : int){
Health = Health - health;
}
// When the enemy is hit animat...
// But i can seem to make Yeild To WORK....!!!!!!
function gotHit(){
animation.CrossFade("resist");
yield WaitForSeconds(1);
}
function Test(){
Debug.Log(this.name);
}
Your pointers are appreciated
You're going to need a Spawner to spawn some new enemies for you when all (?) of your existing enemies are dead.
So your spawner should probably spawn them initially. This way it has a reference to them all, and your enemies will notify the spawner when they're killed. As soon as the spawner knows that there are no enemies left in the scene then it will spawn some more.
Thanks iwaldrop Thats get me to few questions......
How do you check if your enemy still exist in the scene ?
When you generate new enemies do you generate each enemy from a different Prefab or from object etc...?
Do i generate a unique enemy so it will not conflict with other enemies...?
Thanks in advance... Its really puzzle me how to achieve it...
Answer by iwaldrop · Feb 02, 2013 at 06:38 PM
You don't need to check the scene for enemies, ever, if your spawner spawns them in the beginning and is then informed by the enemy on their death. If it spawns 10 enemies and all ten let it know that they're dead then the spawner knows that there are no more enemies in the scene.
You can use a single enemy prefab or, if you have multiple enemy types, several prefabs chosen randomly, weighted by type, or manually designed.
Each GameObject that you instantiate is already unique, so take that as you will.
You haven't asked about the where of things, and that is actually the hardest part. I would probably have a few spawn markers and then spawn your enemies in a random angle and distance from that center point. I haven't done that part for you in my example, but it should get you moving in the right direction.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spawner : MonoBehaviour
{
public int maxInstances;
public bool respawnAtZero;
public GameObject[] prefabs;
public GameObject[] spawnPoints;
private List<GameObject> activeSpawn;
void Awake()
{
activeSpawn = new List<GameObject>(maxInstances);
Spawn();
}
void Spawn()
{
for (int i = 0; i < maxInstances; i++)
{
Transform spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
activeSpawn.Insert(i, GameObject.Instantiate(prefabs[Random.Range(0, maxInstances)], spawnPoint.position, spawnPoint.rotation));
activeSpawn[i].SendMessage("NotifyMeOnDeath", this, SendMessageOptions.DontRequireReceiver);
}
}
void HandleOnSpawnDied(GameObject go)
{
activeSpawn.Remove(go);
if (activeSpawn.Count == 0)
Spawn();
}
}
All you have to do to make this work is make a complimentary script to place on your enemies which will listen for the "NotifyMeOnDeath" message and know what to do with it. Namely, it should tell the spawner that it's dead!
Your answer
Follow this Question
Related Questions
Simple 2D Enemy AI 3 Answers
Make object move in random directions 1 Answer
InvokeRepeating with a variable? 2 Answers
Generating objects randomly, only one will Instantiate 0 Answers
How to generate random number that will not be repeated? 2 Answers