Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sugar Mommy · Jul 11, 2015 at 12:15 PM · 2dplatformerrespawncheckpoint

[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:

alt text

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PixelSpartan · Jul 11, 2015 at 12:20 PM 0
Share

Why are you using IEnumerator??

Try using a normal void

avatar image PixelSpartan · Jul 11, 2015 at 12:22 PM 0
Share

you should put respawning and diying in diffrent voids

avatar image Umresh · Jul 11, 2015 at 12:57 PM 0
Share

Check if Respawn() call is locked in some kind of loop.

avatar image Sugar Mommy · Jul 11, 2015 at 01:48 PM 0
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Why is my gameObject falling so slow? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges