- Home /
Die on collision doesnt work
i have a code for health and in the code i have a code so i would die on collision with the enemy, the enemy has a riged body and my charector does too and we both have a charector.collider() or whatever it is but the code is not working please help! the code is:
var PlayerHealth: float = 10.0;
var EnemyDamage: float = 10.0;//(damage per sec)You may change it to kill player quickly
function OnTriggerStay(enemy: Collider){
if(enemy.CompareTag("Enemy"))
{
PlayerHealth-=EnemyDamage*Time.deltaTime;
}
if(PlayerHealth<0)
{
Debug.Log("Player died.You lost!");
Destroy(gameObject);
}
}
The script is attached to the player gameobject yes? You would see it listed in the inspector. Also check that the enemy gameobject is tagged "Enemy" (it is caps sensitive!).
ya the gameobject is tagged "Enemy" and the script is attached to the player gameobject
"...we both have a charector.collider() or whatever it is..."
Details like this matter a lot. If you're asking other people for help, please have the courtesy to put some effort in yourself and describe the scenario fully and accurately.
oh thanks it wasnt attached to the object it was attached to the controlles
but when i build the game my charector doesnt die it just keeps going!!!!
Answer by eshonbel · May 26, 2014 at 09:16 PM
Might as well try loading a new scene instead of destroying, you could load a level. 1. Make a level, call it Respawn 2. Change this:
Destroy(gameObject);
//to this
Application.LoadLevel("Respawn");
//and then add Respawn to build settings.
Answer by Java666 · May 28, 2014 at 09:40 PM
i think you are just missing a collider you need a physic collider and have it check if trigger in that collider if using a trigger function
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Scale FPS character/view throws everything out of wack, help? 1 Answer
How to make blocks collide - help fast please 1 Answer
Damage script 0 Answers
Scavenger script woe... 1 Answer