How to make player respawn without resetting scene
I want to make my player respawn when hit by enemy x amount of times without resetting the scene. How would I go about doing this? This is my first time using unity so try to keep responses as simple as possible.
Comment
Answer by awest · Jun 21, 2016 at 07:04 PM
You can store initial values for the player. And use them to 'reset' the object when the time comes. You can subtract health using OnCollisionEnter or OnTriggerEnter if you don't already have a plan for that. An example,
public int maxHealth; // assign public variables in inspector.
private int health;
public Vector3 startPosition;
void Start() {
health = maxHealth;
}
void Update() {
if (health <= 0) {
transform.position = startPosition;
health = maxHealth;
}
}
void OnTriggerEnter(Collider other) {
if(other.tag == "Enemy"){
health --; // subtract 1 from health
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612082031im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Huge Health and Damage Script Problem 0 Answers
Fall Damage script not working 1 Answer
OverslapSphere dont detect enemyhealth 0 Answers
Platformer Jump Script Without AddForce 0 Answers
Interesting Script Problem 1 Answer