- Home /
How to destroy and respawn the player after loosing a life?
I want to destroy the player object and reset it to the original position it was when the game start. I also want to display a game over screen after the live reaches zero.
public class ShipPlayerController : MonoBehaviour {
// wrecked into an enemy?
if (other.tag == "EnemyShip") {
ModShield(-25);
if ( CurShield <= 0) {
ModHealth(-1);
}
if( CurHealth <= 0) {
ModLives(-1);
}
}
Answer by bubzy · Oct 22, 2014 at 08:56 PM
why destroy the player? just move it back to the start position. if you want to make an explosion or something, just move the player offscreen, it will save resources.
//playerscript
public Transform startLocation; //drag a empty gameObject into this on the inspector, then move the empty gameObject to where you want the player to "respawn"
//then
if (CurHealth <=0){
ModLives(-1);
//do some explody thing
//if you need to "hide" the player just use something like
//transform.position = new Vector3(-999f,-999f,-999f);
transform.position = startLocation.position; // move the player to the position of the empty gameObject
}
also I see a lot of people naming their variables with a capital letter, while this is absolutely your own choice, its nicer to start with a lowercase letter and capitalise the second word(if there is one) not only does it look nicer, but you can avoid clashes with reserved words(sometimes). just a thought :)
look at GUI options for the game over screen, there are some great tutorials available. the code would be
if(lives == 0)
{
//display game over screen
}
but I guess you could work that our for yourself :)
all the best with your game
Thanks. I already have a game over screen the problem is that I need to get the screen to fade to the scene
There are a lot of tutorials online about screen fading. See: Screen Fader Tutorial