- Home /
how do I make smart checkpoints?
I have made checkpoints in unity and they work ok (they respawn the character when the character dies) but all the enemies that my character has killed so far has respawned with the character. How do I make the checkpoint know not to spawn enemies after they die?
here is my playerpos script (which makes the player move to the last checkpoint position)
public class PlayerPos : MonoBehaviour
{
private GameMaster GM;
void Start()
{
GM = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
transform.position = GM.lastCheckpointPos;
}
}
here is my checkpoint script:
public class CheckPoint : MonoBehaviour
{
private GameMaster GM;
private void Start()
{
GM = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
GM.lastCheckpointPos = transform.position;
}
}
}
here is my Gamemaster script:
public class GameMaster : MonoBehaviour
{
// Start is called before the first frame update
private static GameMaster instance;
public Vector2 lastCheckpointPos;
void Awake()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(instance);
}
else
{
Destroy(gameObject);
}
}
}
How do you respawn the player? Do you reload the scene? If so, I think it would be better to just reset the stats oft the player. Otherwise I'm not sure why the enemies geht respawned too. Could you descripe it further?
I respawn the player by reloading the scene. I have updated my post to put my checkpoint script
dont respawn the player reloading the scene simply move it to the desire position
Ok, thx. So as already said I would just reset the stats of the player and put him back to the last checkpoint or destroy the player and load it from a prefab again(make sure to do that in the same frame). Reloading the scene is also more intense for the computer. In my opinion the only reason to reload the scene, while not resetting the whole level (respawn enemies as well), would be if you have another scene you use before respawning the player. In that case I can't help you a lot since I have never die this before and currently can't test with Unity.
Answer by Alphacreations · Jul 19, 2020 at 10:24 PM
i made a new vector 3 and had 1 variable ; respawnpoint
i made it if the player collides with the checkpoint, the respawnpoint is equal the the transform.position of the checkpoint
i made it so that when the player died he reset his position like @Acdia said
Your answer
Follow this Question
Related Questions
Force enemy to fire at a specific point, == appears not to work. 2 Answers
fps shooting enemy 1 Answer
Enemy needs to shoot precisely at player 2 Answers
how can i destroy a cube when i press a gui button??? 1 Answer
Enemy Detects Buildings? 2 Answers