- Home /
Detect different death sources?
Hi, I have a simple Health system for my little enemy.(When health <0 die) How do I detect different death types/last source that killed the enemy? Like if the enemy got killed by a bullet/explosion/fall or detecting who fired the shot that killed it? Is there some ways to do this by broadcasting messages or are there other ways of achieving this? Please help in JS, THANKS!
Answer by nhftk12 · May 21, 2011 at 12:52 PM
var lives = 100;
function OnTriggerEnter ( hit : Collider)
{
if (hit.gameObject.tag == "weapon1")
{
lives -= //add lives number; // (can be more/less then the other bullets)
Destroy(hit.gameObject);
//do someting
if (lives <= 0)
{
//dead
print ("killed by weapon1");
}
}
if (hit.gameObject.tag == "weapon2")
{
lives -= //add lives number; // (can be more/less then the other bullets)
Destroy(hit.gameObject);
//do someting
if (lives <= 0)
{
//dead
print ("killed by weapon2");
}
}
if (hit.gameObject.tag == "weapon3")
{
lives -= //add lives number; // (can be more/less then the other bullets)
Destroy(hit.gameObject);
//do someting
if (lives <= 0)
{
//dead
print ("killed by weapon3");
}
}
}
You add this code to the enemy and the enemy will act differantly according to the weapon/bullet that hit him. if his lives <= to 0, it will print who/what killed him.
Answer by Ashkan_gc · May 21, 2011 at 12:16 PM
i know an efficent way that might not be as easy as you like. create a component called deathSource for all objects that can cause death and then in OnTriigerEnter /OnCollisionEnter that you check for death events take the reference of the object that is reducing your health and get it's deathSource component using GetComponent and for each object put it's information like name and deathtype in the component to retreive.
I dont understand what you mean by checking for death events, can you explain it in detail or even post a code sample about what you're pointing against? Thanks!
Your answer
Follow this Question
Related Questions
Sound effect when health=0? 2 Answers
PlayerHealth drops fast and wont reset 1 Answer
C# issue --- code assistance. 1 Answer
Death scene/screen on player Health Script Wont work! 0 Answers