- Home /
How to make my character die
Hi, I am making a 2D sidescroller and my knowledge of javascript is a little limited. Now I am trying to make a area where the player dies if he is to fall of a platform or something like that. I set up a 2D boxcollider and I attached the following script to it:
#pragma strict
//Damage variable
var TheDamage : int = 500;
var Player : Rigidbody2D;
function OnCollision2DEnter(hit : Collision2D)
{
if (hit.gameObject.name == "Player")
{
hit.gameObject.SendMessage("ApplyDamage", TheDamage);
}
}
The code for the damage and death of the character:
#pragma strict
var Health : float;
function Update ()
{
if (Health <= 0)
{
Death ();
}
}
function ApplyDamage (TheDamage : int)
{
Health -= TheDamage;
}
function Death ()
{
Destroy (gameObject);
Application.LoadLevel (Application.loadedLevel);
}
So I made the damage pretty high because the health can very in height but at all times its under 500. If the player collides with the "DeadZone" I want him to be destroyed and I want the level to reset.
Your help will be greatly appreciated.
Answer by yarwest · May 29, 2014 at 02:16 PM
I fixed it already massive facepalm I missplaced the 2D in the OnCollisionEnter2D :-p stupid error....
Your answer
Follow this Question
Related Questions
Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer
how do i break apart a 2d car when it collides with another object? 0 Answers
Make Enemy Solid Object 2 Answers
Get the position of the particular tile involved in a TileMapCollider collision? 0 Answers
Destroy objects when key is pressed 1 Answer