One way to do it is to create a Body- and a BodyPart class. The Body class holds information about each body part where you would assign each element to its corresponding part.
To give you an idea of the concept, the class would look something like this:
class Body {
var health : float = 100;
var minHealth : float = 0;
var maxHealth : float = 100;
var head : BodyPart;
var chest : BodyPart;
var armLeft : BodyPart;
var armRight : BodyPart;
var legLeft : BodyPart;
var legRight : BodyPart;
// etc...
}
class BodyPart {
var collider : Collider;
var health : float = 100;
}
Changing the main health of a body object would look something like this:
function AlterHealth (healthChange : float, body : Body) {
// Additively alter the health of passed body
body.health = Mathf.Clamp(body.health+healthChange, body.minHealth, body.maxHealth);
}
Creating a body object with these abilities would look like this: var body : Body = new Body();
Each part of the body would have a collider which you assign to the body object. You would want to catch collisions with a body part collider, preferably with a script which sends information to the main body script. Something like this:
var thisBody : BodyScript; // The main Body Script of this object
function OnCollisionEnter (collision : Collision) {
if (collision.collider.CompareTag("Collidable Object")) {
BodyScript.AlterBodyPartHealth(thisBody.body, collider, collision.relativeVelocity.magnitude);
}
}
Where the BodyScript would have a function, AlterBodyPartHealth, which receives the part and handles it correspondingly to the body object.
static function AlterBodyPartHealth (body : Body, col : Collider, healthChange : float) {
// Main health change for non-vital limbs
var mainHealthChange : float = healthChange*.25;
// Additively alter the health of passed body's body part and change the mainHealthChange for vital body parts
switch (col) {
case body.head.collider: body.head.health -= healthChange; mainHealthChange*=4; break;
case body.chest.collider: body.chest.health -= healthChange; mainHealthChange*=2; break;
case body.armLeft.collider: body.armLeft.health -= healthChange; break;
case body.armRight.collider: body.armRight.health -= healthChange; break;
case body.legLeft.collider: body.legLeft.health -= healthChange; break;
case body.legRight.collider: body.legRight.health -= healthChange; break;
}
// Alter the main health as well
AlterHealth(mainHealthChange, body);
body.health = Mathf.Clamp(body.health+healthChange, body.minHealth, body.maxHealth);
}
I strongly suggest to start with something easier and work your way through understanding of the built-in classes and their correlation before you tackle something like this. There’s a lot around this as well, handling collisions the correct way is just one tricky part to become friends with at first - as well as creating the skinned mesh avatar.
For more information about how you solve the GUI to this, please see the GUI Scripting Guide and ask another question if you have trouble when you’re there and we’ll help you through it.