Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

What to do when your player dies?

Discussion in 'Scripting' started by BigLebow, Jul 17, 2021.

  1. BigLebow

    BigLebow

    Joined:
    Dec 19, 2019
    Posts:
    7
    Hi, Im currently making a top down shooter. I'm curious to know how to handle my player dying. Basically, I don't think I want to destroy my gameObject, or just teleport them to some spawn location immediately, Or teleport them at all. Instead I think it would be better to have a spectator mode of sorts. Is there a way to disable components like collider2D and rigidbody2D so that my player becomes uninteractable by other players. Similar to how AmongUs handles dead players. Im not looking for specific scripting solutions, just what would be common practice in these scenarios?
     
  2. DrHerringbone

    DrHerringbone

    Joined:
    Jan 3, 2021
    Posts:
    13
    Yes you could run a function (assuming that the player has just one rigidbody/collider)
    Code (CSharp):
    1.  
    2.  
    3. public class PlayerDied : MonoBehaviour
    4.     {
    5.          public bool isPlayerDead;
    6.  
    7.          private gameObject Player;
    8.  
    9.          void Start()
    10.               {
    11.                   Player = GameObject.FindObjectWithTag("Player") //or you can just make the player variable public and drag and drop your player GameObject in the editor but this is neater... I think lol
    12.               }
    13.  
    14.         void update()
    15.              {
    16.                   if(isPlayerDead){
    17.                        Player.GetComponent<Rigidbody2D>().Simulated = false;
    18.                    }
    19.               }
    20.          }
    21.  
    22.  
    23.  
    DISCLAIMER!!!:
    (I think this code is 100% correct but I'm no Bill Gates and I wrote it off the top of my dome)
     
    Last edited: Jul 17, 2021
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    All of these are valid approaches. Depends if the player can respawn or not, depends if the level completely resets itself, or if some other feature can revive them (such as "Watch a video now to revive here!"). etc.

    RBs and RB2Ds cannot be disabled but the same can be achieved by marking them as kinematic or not simulated, or just utterly deleting them. It really kinda comes down to the type of experience you're making.
     
    BigLebow and Yoreki like this.
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    It's incredibly common in games to swap one GameObject for another on death, too. So you could spawn a Player_Dead prefab with different logic without having to selectively enable/disable a bunch of things. Same for the enemies too.
     
    BigLebow likes this.
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    If it is networked you probably don't want to go around destroying and recreating things, and instead just leave a corpse object there and recycle the player.
     
    BigLebow likes this.
  6. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Just disable the dead player's GameObject (so it is not visible to the camera) and make the camera follow the action elsewhere. That is how my Game Over screen works (it also shows the Play Again button).

    The player sees the debris from his exploded spaceship for a few seconds, then the camera that was following him starts following something still in the fight. The player can watch or play again. A less arcade experience would be to have the player respawn without resetting the entire game.
     
    BigLebow likes this.
  7. BigLebow

    BigLebow

    Joined:
    Dec 19, 2019
    Posts:
    7
    Thank you all for your great recommendations!


    I think this is the route I will take!! Having a Prefab to swap out would be incredibly useful for what I'm going for, Thanks!

    This was my thought too. How would you go about recycling the player? What would that logic look like? Something like this maybe: Player Dies > Instantiate Gravestone > Disable Collider and Sprite Renderer > Empty Inventory > Set a Respawn Timer ? Or something like that, just looking for technical ideas on this process.

    I didn't know you could Disable an entire GameObject! That might be useful I'll try that. I've been disabling the Collider and Sprite Renderer with some success.