- Home /
How kill an enemy and respawn after die
Hello, iI'm making a 3D game and I want when enemy's life reaches 0 this disappear and respanw in the same area where started when the game starts.
Here I put the script of life
Thank you
1 var saludActual=100; var
saludMaxima=100.00;
var largoBarra=0.0;
5
function Start () {
largoBarra=Screen.width/10; }
10 function Update () { AjustarSalud(0);
}
function OnGUI() { GUI.Box(new
15 Rect(1150,30,largoBarra,20),saludActual+'/'+saludMaxima);
}
function AjustarSalud(salud) {
20 saludActual+=salud;
if(saludActual<0) {
25 saludActual=0; }
if(saludActual>saludMaxima) {
saludActual=saludMaxima; }
30 if(saludMaxima<1){ saludMaxima=1;
}
largoBarra=(Screen.width/10)*saludActual/saludMaxima;
35
}
Answer by Mimumu84 · Sep 07, 2014 at 07:03 PM
You could create another script attached to an empty GameObject that will help to count the number of enemies in the area. When the 'total number of enemies' is below the maximum number of enemies, you can instantiate your enemy from the GameObject or a specific spawnpoint.
In your enemy script, when your enemy health = 0, you can access the script attached to the empty GameObject using 'GetComponent("myscript")" and subtract the current number of enemies by 1. Then destroy your enemy transform. Add a bit of time between your subtraction with 'current number of enemies' and destoying your transform with "yield WaitForSeconds(float).
Answer by ComeSweetDeath · Sep 07, 2014 at 07:09 PM
var saludActual=100;
var saludMaxima=100.00;
var largoBarra=0.0;
private var respawnPoint : Transform;
function Start () {
largoBarra = Screen.width/10;
respawnPoint = transform.position;
}
function Update () {
AjustarSalud(0);
Respawn ();
}
function OnGUI() {
GUI.Box(new Rect(1150,30,largoBarra,20),saludActual+'/'+saludMaxima);
}
function AjustarSalud(salud) {
saludActual += salud;
if(saludActual<0) {
saludActual=0;
}
if(saludActual>saludMaxima) {
saludActual=saludMaxima;
}
if(saludMaxima<1){
saludMaxima=1;
}
largoBarra=(Screen.width/10) * (saludActual/saludMaxima);
}
function Respawn () {
if (saludActual <= 0){
transform.position = respawnPoint.position;
saludActual = saludMaxima;
}
}
A tiny modification based on your script. Basically it simply records the position when your character started. And when it's health reaches 0, it is teleported to the starting location with max health.
Your answer