Hi, all,
I’m trying to figure out how best to destroy an enemy by first shooting pieces of its body. Should I use a hit point strategy and make each piece subtract from the main body when hit?
Hi, all,
I’m trying to figure out how best to destroy an enemy by first shooting pieces of its body. Should I use a hit point strategy and make each piece subtract from the main body when hit?
This would take off the same pieces each time, starting with piece ‘4’ and going to piece ‘0’. Each piece would fall off at the same point as every other piece, IE: every 20 hit points, a new piece would fall off.
numPieces = 5;
maxHitPoints = 100;
removeThreshold = maxHitPoints / numPieces--;
currentHitPoints = 100;
OnGetHit(){
//calcualte damage however you feel like
//keep in mind, dont drop below 0
currentHitPoints -= damageAmount;
if(currentHitPoints % removeThreshold <= numPieces){
//remove piece 4,3,2,1,0 (in that order)
//0 means your dead 4 is first piece to remove.
switch(numPieces){
case 0:
//remove helm
break;
//etc for all others
}
numPieces --;
}
}
Other things you could do is to remove some pieces at specific times, such as at 90 hit points, remove the glove, at 75 hit points, remove the chest piece, at 50 hit points remove the helm, etc.
Still another idea would be to randomize which piece fell off. So each time the user fought the same monster, the pieces would fall off in different order.
Or you could create a buffer, which if met, there is a 1/4 chance a random piece would fall. This chance is checked each time the buffer isn’t reset, and once a piece falls, the buffer resets so they have to lose more hit points until the buffer fills again.
IE:
hitLossBuffer = 0;
maxHitBuffer = 10;
if(hitLossBuffer > maxHitBuffer){
//sorry, I don't know the Unity Math.rand function name
if(randomValue > .75){
//make a random piece fall
hitLossBuffer = 0;
}
}
It really depends on what you want to make. If it is like a boss, where you want it to be the same experience every time, go with something like the first example. If you want it to be random, etc. Then be creative
Also, you may want to use percentages and not specific hitpoint numbers, so you could reuse the code for different monsters, and not have to adjust every hit point line.
Bunzaga, you rock! That is exactly what I was looking for! I’ve already implemented it in my scene and it is perfectly suited to what I am going for.
Would you have any recommendation for doing the opposite effect, such as shooting each body member, effectively applying damage to the main body.
For example: if you have five spheres and shoot each one off the main body dies after the last one.
Thanks again for you help!
Well that one would be a little different. Each body part would have its own hit points.
There are different ways to handle this. The most easy, just use an int to store how many body parts are attached. If the number is <= 0 , blow up the body. As each body parts hit points reach 0, it reduces the partCount by -1…
You would need two separate scripts for this.
//main body
private var partCount = 5; //to prevent direct access
public function onPartFall(){
partCount-=1;
if(partCount <= 0) {explode();}
}
private var hitPoints = 25;
public function onGetShot(){
//implement damage amount either being passed in,
//or a random number, set range, whatever
hitPoints -= damageAmount;
if(hitPoints <=0){
explode();
//mainBody.onParFall();
}
}
The way you would implement a lot of this is dependant on how you have your scene setup. You could create public variables, link the child body parts to the main parent with the Unity UI, etc. That way you don’t need to do a Find(). So that choice is up to you.
Another way which came to mind, is if you don’t know anything about the children or parent before runtime. You could create a hashMap (or array I guess) and each Key = one body part. Then when you destroy a body part, you delete the key from the array/map and check if the array/map size is 0, if it is 0, blow up the main body.
This would be cool for something where a main body could pick up whatever random pieces were next to it in the game.
Edit:
It just occured to me, if you want to have only one health bar, then as each body part is shot, you would add all the child parts health together and display that as the health.
Edit2:
Sorry, I keep thinking of stuff as I am re-reading the posts. Another idea, you could have a ‘main body health’ too. The user has to shoot off all the child objects, then he has to shoot the main body too.
To prevent the main body from getting damage before the child objects have fallen, you could put an if check to see if the partCount <=0. If it is, now take away the main body hit points. If it is not, then do nothing.
Unity has some cool features for things like this built into it too. You could just change the layer effected by bullets too. When all the child parts are destroyed, change the main body layer to now be considered in collisions with bullets.
Hi, Bunzaga,
Thanks for all of your insight and consideration to that question, you really went above and beyond! I think I will try a combination of your script with something that I was working on and experiment with it this evening.
Thanks again for all of your time and thought, I appreciate it very much!
Best,
Greg