- Home /
[2D] [C#] Player dying at respawn at checkpoint
Hi
I'm making a 2D platformer game, and I keep running into a fairly simple problem, but for me it's just hard to find out the cause of it.
Sometimes when the player spawns (either at startup or when respawning at a checkpoint), he gets stuck in some kind of loop that keeps killing him over and over again. This doesn't happen often, but when it does, it's obviously quite annoying.
Below you will find the code I've used for the checkpoints, and the coroutine I used to respawn the player. The thing is, I know they work, because most of the time nothing's wrong, so it has to be some kind of tiny mistake I never noticed.
Also, this might just be a coincidence, but I feel like this issue happens more often the longer I play a session. Again, this might be completely wrong.
Checkpoints
public class Checkpoint : MonoBehaviour
{
public LevelManager levelManager;
void Start()
{
levelManager = FindObjectOfType<LevelManager>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "player")
{
levelManager.currentCheckpoint = gameObject;
Debug.Log("Activated checkpoint at" + transform.position);
}
}
Respawning
public void RespawnPlayer()
{
StartCoroutine("RespawnPlayerCo");
}
public void KillCo()
{
Debug.Log("Player died.");
Instantiate(deathParticles, player.transform.position, player.transform.rotation);
player.enabled = false;
camera.isFollowing = false;
renderer.enabled = false;
ScoreManager.AddPoints(-pointPenaltyOnDeath);
}
public void RespawnCo()
{
Debug.Log("Player respawned.");
player.knockBackCount = 0;
Instantiate(respawnParticles, currentCheckpoint.transform.position, currentCheckpoint.transform.rotation);
player.enabled = true;
camera.isFollowing = true;
renderer.enabled = true;
healthManager.FullHealth();
healthManager.isDead = false;
player.transform.position = currentCheckpointPosition;
}
public IEnumerator RespawnPlayerCo()
{
KillCo ();
yield return new WaitForSeconds(respawnDelay);
RespawnCo ();
}
Does anyone have an idea what might be going wrong here? Just in case it's necessary, here's a screenshot of how I have my checkpoints set up in the scene:
Why are you using IEnumerator??
Try using a normal void
you should put respawning and diying in diffrent voids
@PixelSpartan I'm using a coroutine so that I can have a small pause in between the player dying and respawning. I did make two different void methods for killing and respawning now, but it still doesn't work :(
@Umresh when calling Respawn(), all I do is start the coroutine, so I would be surprised if the issue was in there... Then again, I'm not exactly experienced in program$$anonymous$$g, so if you still think I should check something out, please let me know.
Thanks for both your answers, I've edited my main post to show some more information on how everything looks now.
Answer by Sugar Mommy · Jul 27, 2015 at 07:24 PM
Figured it out, I had a few statements in the wrong order. I should've changed the player's position before actually re-enabling him, while this used to happen after all the other things.
Your answer
Follow this Question
Related Questions
animation 2d platformer 2 Answers
switching between different screen boxes 2 Answers
2D Buttons 1 Answer
Play two 2D animation together? 2 Answers