How to kill a player in a 2d game

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

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

1 Like

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()

Like this?

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

Yes exactly like that you destroy the player

ok thx

thank you so much I’m a fool who didn’t know how to do this

it gives me a error that the name destroy doesn’t exist in the current context, how can i solve that?

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?

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/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

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

How to use code tags: https://discussions.unity.com/t/481379