- Home /
Checkpoint script problem
So I'm making a checkpoint script with empties in the game that are represented by GameObjects spawn, checkpoint1, checkpoint2, etc. The player respawns at the checkpoint, however they get stuck there and they can't move or fall. I checked with my teacher and they couldn't find exactly what the problem was either. I'm thinking that maybe the player is somehow snapping to the empty and being pinned there, as the movement script still functions, but the player doesn't move. Please let me know if you think you know what the problem could be, any input is appreciated. Many thanks!
bool alive=true; Vector3 spawnPoint;
int numberDeaths; public GameObject playerExplosion; float timer; int checkpointNumber; public GameObject checkpoint1; public GameObject checkpoint2; public GameObject spawn; bool death; Renderer[] playerMesh; public float offset; public GameObject mesh;
void Start()
{
checkpointNumber=0;
alive=true;
death=false;
GameObject spawnObject = spawn;
spawnPoint = spawnObject.transform.position;
}
void Update ()
{
//playerMesh = GetComponentsInChildren <Renderer>(renderer.enabled);
if (death)
{
if (checkpointNumber==0)
{
if (alive)
{
GameObject spawnObject = spawn;
transform.position=spawnPoint;
spawnPoint = spawnObject.transform.position;
death=false;
timer=0;
collider.enabled=true;
}
if (!alive)
{
timer += Time.deltaTime;
}
if (timer > 1.3 && !alive) {
GameObject spawnObject = spawn;
spawnPoint = spawnObject.transform.position;
transform.position=spawnPoint;
alive=true;
timer=0;
death=false;
collider.enabled=true;
}
}
}
if (checkpointNumber==1)
{
if (alive)
{
GameObject spawnObject = checkpoint1;
transform.position=spawnPoint;
spawnPoint = spawnObject.transform.position;
death=false;
collider.enabled=true;
}
if (!alive)
{
timer += Time.deltaTime;
}
if (timer > 1.3 && !alive) {
GameObject spawnObject = checkpoint1;
transform.position=spawnPoint;
spawnPoint = spawnObject.transform.position;
collider.enabled=true;
death=false;
alive=true;
timer=0;
}
}
if (checkpointNumber==2)
{
if (alive)
{
GameObject spawnObject = checkpoint2;
transform.position=spawnPoint;
spawnPoint = spawnObject.transform.position;
death=false;
collider.enabled=true;
}
if (!alive)
{
timer += Time.deltaTime;
}
if (timer > 1.3 && !alive) {
GameObject spawnObject = checkpoint2;
transform.position=spawnPoint;
spawnPoint = spawnObject.transform.position;
collider.enabled=true;
alive=true;
timer=0;
death=false;
}
}
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "checkpoint")
{
checkpointNumber=checkpointNumber +1;
other.gameObject.collider.enabled =false;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "death")
{
death=true;
numberDeaths=numberDeaths +1;
}
if (collision.gameObject.tag == "boom")
{
Instantiate (playerExplosion, transform.position, transform.rotation);
alive=false;
death=true;
numberDeaths=numberDeaths +1;
mesh.SendMessage ("RenderStatus");
collider.enabled=false;
}
}
Answer by Kriogenic · Feb 06, 2015 at 02:32 AM
One problem could be that the if(death) statement at beginning of update is ONLY around checkpoint 0 and none of the others. so the other checks are happening even when you are not in death state.
Thanks for your suggestion, that did fix a lot of the problems. However I still have a problem where I respawn at the previous checkpoint once before respawning at the new checkpoint. Any ideas why that could be the case?
That I think has to do with this.
transform.position=spawnPoint;
spawnPoint = spawnObject.transform.position;
you are setting the position first and then changing the spawnPoint. try swapping those 2 lines around each time it appears.
Thank you so much, that did it. I thought everything in an if was called at same time, but I see how order can make a difference. Thanks for solving my problem!
Your answer
![](https://koobas.hobune.stream/wayback/20220613182249im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help with SendMessage 1 Answer
Why doesnt my checkpoint system work? 1 Answer
Moving Player After Falling Certain Distance [Beginner/C#] 1 Answer