- Home /
Explode object after being shot based on health decreasing (particle explosion)
I'm new the unity world and I'm a rather experience scripter in other languages but aside from that I was hoping that someone would be able to point a new member of the UND community in the right direction.
I simply want to take an object which has 100% health and have it's health deteriorate base on X number of shots/hits, and when its health equals 0% it will then explode using a particle system of some sort. I'm looking for a tutorial or sample/example of this exact creation, as from there I can pick up and learn from it. If it's easier for someone to share a super simple example scene of just a sphere exploding upon X number of shots/hits that would be greatly appreciated. Any help is great and I look forward to becoming a contributor and frequent member of the community here at UDN.
How do i know when people post comments if they are done in c# or java?
I'd like to develop my game using c# as its something I want to learn in the process.
Answer by Piflik · Jul 17, 2012 at 01:14 AM
You'd need a health variable with a certain value. Then you add a OnCollisionEnter or OnTriggerEnter function and test if the colliding object is a projectile and if so decrease the health value by the projectile's damage. Lastly you test if the health is smaller than 1 and then instantiate a particle system and delete or deactivate the player.
Example (the actual script I am using in my game - minus some stuff that wouldn't help and might even confuse):
function OnTriggerEnter(item : Collider) {
if(item.tag == "Projectile"){
if(item.GetComponent(Projectile).owner != "player" && !shieldActive && !invincible){
PlayerStatus.plHealth -= item.GetComponent(Projectile).damage/PlayerStatus.multiplier;
if(PlayerStatus.plHealth < 1 && !plDead) {
Instantiate(deathFX, transform.position + Vector3(0, 15, 0), Quaternion.identity);
plDead = true;
PlayerStatus.lives--;
statics.playerActive = false;
yield WaitForSeconds (1.5);
respawn();
if(PlayerStatus.lives < 1){
GameObject.FindWithTag("MainMenu").SendMessage("gameOverFN"); //GAMEOVER
plDead = false;
}
}
}
}
}
Do you know of any good tutorial or youtube videos that would explain so supply this in its most simplest form. In my case, an example of a sphere with a 100% health which gets hit by a projectile that does a damage of 35 percent. So the sphere dies in 3 hits. I'd love to see how this is done. I'm not sure how to set up projectiles or and object with health yet and that is what I'm looking to learn how to do. I've got all my objects set up which I want to have health.
Look for TornadoTwins in youtube, they have a 40 videos tutorial for creating a shooter type game.