Day 5 Solutions
Respawn / Checkpoint SystemThis is a simple system for respawning the player at a checkpoint.
If the player runs out of health, you would call “RespawnAtCheckpoint()” to send them back.
Alternatively, if you dont have a health system, you can just send the player back whenever they touch an enemy or hazard with getcomponent and oncollisionenter2d / ontriggerenter2D.
public class Respawn : MonoBehaviour
//we will use a vector2 to store the position of the last checkpoint
// Start is called before the first frame update
//set the first checkpoint position
//right where the player starts, so if they die before hitting a checkpoint
//they go back to the start
Checkpoint = transform.position;
//use this function to respawn at the last checkpoint
//if using health, make sure to also reset health
public void RespawnAtCheckpoint()
transform.position = Checkpoint;
//use this function to set a new checkpoint
//I would write another script that calls
//this in ontriggerenter2D with getcomponent
//like how our inventory system works...
public void SetCheckpoint(Vector2 position)
Checkpoint = position;
Load a SceneFor this to work you NEED TO ADD using UnityEngine.SceneManagement; to the top of your script, and also make sure ALL THE SCENES YOU WANT IN YOUR GAME ARE ADDED TO BUILD SETTINGS
//MAKE SURE you include UnityEngine.SceneManagement, it is necessary to load levels!
public class LoadLevelDemo : MonoBehaviour
public string LevelNameToLoad;
//use SceneManager.LoadScene like this to load levels