- Home /
Lose health and respawn after falling
I'm making a 2d platformer and I'm looking for something like the Shantae games where instead of having lives the player to falls off cliffs and loses a heart after respawning to the stage. I have the game over and respawn system set, but the player ends up recovering lost health after the respawn as opposed to losing.
Please help!
My kill script: using UnityEngine; using System.Collections; public class KillPlayer : MonoBehaviour { public Player player; // Use this for initialization void Start () { player = FindObjectOfType<Player> (); } // Update is called once per frame void Update () { } void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Player") { player.Death (); player.Damage (1); } } }
and my respawn code: public void Respawn () { StartCoroutine ("RespawnCoroutine"); } //Routine for respawn dalay time public IEnumerator RespawnCoroutine() { gamePlayer.gameObject.SetActive (false); yield return new WaitForSeconds (respawnDelay); gamePlayer.transform.position = gamePlayer.respawnPoint; gamePlayer.gameObject.SetActive (true); } }
Hello there,
You need to provide more information if you want us to be able to help. For example, how did you setup your respawn system? Please share the relevant code as well (edit your original post) so we can have a look!
Cheers,
~LegendBacon
Sorry, I've been at work but I believe I've set it to show my methods
You may need to also post your Player script Could you track and find all the places where you set the health?
Answer by Legend_Bacon · Mar 23, 2018 at 12:29 AM
Hello there,
based on your player code, you seem to be reloading the scene whenever your player die. Doing that will restart the scene EXACTLY how it was the first time, player health included... Unless you have it saved externally.
A simple fix would be to save the player's health to PlayerPrefs. Then, on Start you can just set the currentHealth to the saved value. Note that you will have to manually manage this saved health.
I hope that helps!
Cheers,
~LegendBacon
Ah, ok I see what you mean! Ill re work some things and check again Thank you!
Answer by Baktillus · Mar 22, 2018 at 04:33 PM
As a fan of simple solutions:
Make a deathplane. Some trigger below the edge of your screen. When the character falls out of screen, call a script that resets their position and damages them an appropriate amount.
@Baktillus Ive done all that, but the respawn restores health when it activates
Oh I see! I meant not to use your respanw script (as it seems to reset all that) but ins$$anonymous$$d just reset the palyers position and deal damage to them.
Legend_Bacon already gave a great answer for using your respawn script :)