- Home /
Checkpoints in a platformer
Sincere greetings ladies and gentlemen. I require a checkpoint system for my game in development, and I am completely new to unity so this is proving a little difficult. I am able to send the player back to the last checkpoint they touched without problems. The problem I have however is that it is also necessary for all of the enemies in the level to reset and return to their original positions, which I am clueless about doing. Any help or advice would be greatly appreciated. Thank you.
Answer by redeemer · Jun 02, 2015 at 12:07 PM
If you only need to restore the positions at the last checkpoint, you could try the next approach :
Declare a variable in your enemy's script such as "lastCheckpointPos", you will update this var each time the player gets to a checkpoint.
Create a function to save that last position, such as "SaveLastCheckpointPos()", you have to call this function in all your enemies when the player reaches the checkpoint.
Create a function in that same script such as "RestoreLastCheckpointPos()", where you set the enemy's actual position to the "lastCheckpointPos"
And finally, when you set your player to reappear in the last checkpoint, you must call RestoreLastCheckpointPos() for all your enemies and there you go...
I assume you are controlling the return to the checkpoint in the player's script, I would add something like this in the player script or where you control the checkpoints :
GameObject[] enemiesInScene;
void Start() {
enemiesInScene = GameObject.FindObjectsWithTag("Enemy");
}
//I assume you would have a funtion to set the player
void RestoreToLastCheckpoint() {
//Your code for repositioning the player last checkpoint
foreach(GameObject enemy in enemiesInScene) enemy.GetComponent<EnemyScript>().RestoreLastCheckpointPos();
}
//I assume you would have a funtion to save the last checkpoint
void SaveLastCheckpoint() {
//Your code for saving the last checkpoint reached
foreach(GameObject enemy in enemiesInScene) enemy.GetComponent<EnemyScript>().SaveLastCheckpointPos();
}
And then in the enemy's script :
Vector3 lastChekpointPos;
void Start() {
lastChekpointPos = transform.position;
}
public void SaveLastCheckpointPos() {
lastChekpointPos = transform.position;
}
public void RestoreLastCheckpointPos() {
transform.position = lastCheckpointPos;
}
Hope this helps.
You're welcome and please don't forget to mark the answer as accepted if it helped you solve the problem. If on the other hand the answer does not apply for this case for any reasons we'll work on a different approach ;)
Answer by santiandrade · Nov 19, 2015 at 12:28 PM
Hi! You can use my recent tutorial about the creation of a checkpoints system with Unity: https://santiandrade.github.io/Unity-Creating-a-checkpoints-system/
Good luck!
Your answer
Follow this Question
Related Questions
Race checkpoint system small help please :) 1 Answer
How do I create a good Checkpoint system? 0 Answers
Checkpoint GUI enabled then disabled trouble 3 Answers
How do you make a respawn and checkpoint script? 0 Answers
how do I make smart checkpoints? 1 Answer