- Home /
C# Help - Respawn or Reset
I was wondering if anyone could just lend me a moment of there time to help me with a simple problem.
I've being using the Free Fantasy AI that you can download from the unity asset store, within the game package there is a health script that I have being working on. Within the health script I was able to add a destroy script that would destroy the player gameobject once his/her health is gone.
However the problem I'm having is that I wish to have either the player respawn back at his/her spawn point OR either have the level completely restart?
If anyone could give me some help that would be great. Just in case anyone is wondering that script below is the health script that from the Free Fantasy AI.... public class Health : MonoBehaviour { public float MaxHealth=100; public float CurrentHealth; public bool Invincible; public bool Dead;
// Use this for initialization
void Start () {
//MAKE THE CURRENT HEALTH THE MAX HEALTH AT START
CurrentHealth=MaxHealth;
}
// Update is called once per frame
void Update () {
//IF INVINCIBLE, HE CANNOT DIE..
if(Invincible){
CurrentHealth=MaxHealth;
}
else{
if(CurrentHealth<=0){
CurrentHealth=0;
Dead=true;
}
//MAX HEALTH
if(CurrentHealth>=MaxHealth)CurrentHealth=MaxHealth;
//WHEN DEATH IS UPON HIM
if(Dead){
Destroy(gameObject);
//TELL THE AI SCRIPT HE IS DEAD
FreeAI AI=(FreeAI)GetComponent("FreeAI");
if(AI){
if(AI.IsDead){}
else AI.IsDead=true;
}
}
}
}
}
Go dtí an chéad uair eile, slán leat
Answer by xt-xylophone · Mar 19, 2013 at 07:26 PM
Hi
To the do the respawn, you could add some more class variables to store it's starting location and rotation. Save them in your Start method and then once the character dies, maybe wait a set duration, reset their stats like HP to full and then move them to the saved start position and rotation. So you dont need to destroy the gameobject. With reusing the game object you gotta make sure everything attributed to that character is completely reset too!
For a total restart of the level? A bit more complicated, have a script to 'manage' the level which keeps track of how many characters are dead and if they all are, then just do above and reset everyone or instantiate brand new versions of them in new places, move the player back to a starting location and if everything needed to set up a level is reset it should work no problem.
When I started doing levels the cut was a bit abrupt so I used a FadeOut script from the wiki to get a nicer transition. Good luck!
Your answer
![](https://koobas.hobune.stream/wayback/20220613094715im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Respawning without creating Clones 2 Answers
My script to kill the player and respawn, does nothing? 1 Answer
Alternative to Update 1 Answer