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. Dismiss Notice

How to kill a player in a 2d game

Discussion in 'Scripting' started by Samir21, Jun 14, 2016.

  1. Samir21

    Samir21

    Joined:
    Jun 12, 2016
    Posts:
    5
    So I'm new to unity and I'm making a platformer typed game. I am trying to figure out how to kill or reset the position of my Player and all of his children objects. I don't know were to start, can someone please help me kill my player?
    Thanks
     
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    There is a lot of approaches you can take. You can..

    1.Call Destroy() upon collision with another object
    2.Have a health number that drops when hit/collided until it gets to 0 then call Destroy on it.
    3.Reset the position of the player to starting position once the health drops to 0 (transform.position = resetPoint.position) then reset the health to 100.
    4. If your using guns or lasers, you can use physics.raycast to be used as a weapon and when it intersects with the player, have the health degenerate.
    5. A combination of any of the above methods.

    Collisions and raycast are usually what objects use to interact with. So you can write code by using either method to do what you want.

    If you don't know how to use collisions or raycast, I would recommend going through some Unity tutorials.

    Collision Tutorial
    https://unity3d.com/learn/tutorials/topics/physics/detecting-collisions-oncollisionenter
     
    Samir21 likes this.
  3. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    watch my tutorial series in my signature.

    you want to reset the position? That's simple logic of grabbing the current position of the object and assigning it a "new" position.

    //current position = new position
    transform.position = new Vector3()
     
  4. diegogutimail

    diegogutimail

    Joined:
    Jul 24, 2020
    Posts:
    3

    Like this?


    void OnCollisionEnter2D(Collision2D col)
    {
    if (col.gameObject.tag.Equals(Player))
    {
    destroy();
    }
    }
     
  5. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Yes exactly like that you destroy the player
     
  6. diegogutimail

    diegogutimail

    Joined:
    Jul 24, 2020
    Posts:
    3
    ok thx
     
  7. Thatoneguychris565

    Thatoneguychris565

    Joined:
    Aug 5, 2020
    Posts:
    1
    thank you so much I'm a fool who didn't know how to do this
     
  8. jajopower

    jajopower

    Joined:
    Oct 23, 2020
    Posts:
    1
    it gives me a error that the name destroy doesn't exist in the current context, how can i solve that?
     
  9. PlayStationCoder85

    PlayStationCoder85

    Joined:
    Mar 20, 2021
    Posts:
    2
    you could click on the word, then press alt + enter, then generate field > generate method, then in the {} that should spawn, type:

    private void destroy
    {

    }




    I actually dont know, but maybe someone else can pickup the rest of this thread?
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    No, no, no, no, that's not how it works. Please STOP POSTING to five-year-old threads. It is actually against forum rules!! Don't be a rule-breaker.

    If you have a question, start your own brand-new post... it's FREE!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/