- Home /
Dealing damage to specific body parts?
I've been trying to deal damage to the body parts of a ragdoll for the last couple of hours here and so far I have managed to create a basic damage system that does indeed deal damage to the ragdoll, just not specifically. For some unknown reason no matter where the bullet hits my ragdoll it always deals damage to every body part.
I have a debug set up to tell me which body parts have been hit and how much damage has been done to them, but even if I hit my ragdoll in his left arm, it will tell me that his right leg has been hit for example. I also told the editor to log every object the bullet hits, and despite being certain that it only hit Mesh 5, the upper arm/Shoulder, it continues to deal damage to all body parts. Incase it is relevant, I have the ragdoll grouped into all its different parts and each body part is a child of the group. For example, the group, Hips, Contains Mesh 4 as a child, which is what I've set the editor to collide with. I have the script here, written in JS:
#pragma strict
var bodyContact = ContactPoint;
var Col: Collision;
var playerHealth = 100;
var chestImpact : float = 30.0f;
var headImpact : float = 60.0f;
var hipImpact : float = 20.0f;
var armLegImpact : float = 15.0f;
var footHandImpact : float = 7.0f;
function Update ()
{
if(Input.GetKeyDown(KeyCode.H))
Debug.Log (playerHealth);
}
function OnCollisionEnter (Col: Collision)
{
if(Col.gameObject.name == "Grouped player")
{
Destroy(gameObject);
}
if(Col.gameObject.name == "Mesh9") //Chest
{
print ("Collided with Chest");
playerHealth -= chestImpact;
Debug.Log ("Did" + chestImpact);
}
if(Col.gameObject.name == "Mesh 6" || "Mesh 5" || "Mesh 3") //Left arm
{
print ("Hit left arm");
playerHealth -= armLegImpact;
}
if(Col.gameObject.name == "Mesh 11" || "Mesh 12" || "Mesh 13") //Right leg
{
print ("Hit right leg");
playerHealth -= armLegImpact;
}
}
Answer by Jeff-Kesselman · May 10, 2014 at 10:49 PM
Um your IF syntax is way off.... for instance....
if(Col.gameObject.name == "Mesh 6" || "Mesh 5" || "Mesh 3") //Le
Is saying "If the name is "Mesh6" OR the string "Mesh 5" is true OR the string "Mesh 3" is true.
In Javascript, a string by itself is always true, unless it is null or undefined.
What you need to say is
if( (Col.gameObject.name == "Mesh 6") ||
(Col.gameObject.name == "Mesh 5") ||
(Col.gameObject.name == "Mesh 3"))
This is a good example of somewhere where Unityscript's looseness screwed you up. In C# the compiler would have just objected to it all
In C# you could use a switch statement which would be a lot cleaner. Im not sure if UnityScript does switch on string values or not, but it might. Here are the docs for the switch statement.
In general, as your programs get more complex, you will find its significantly easier to shoot yourself in the foot with UnityScript then C#.
I will probably end up using C# eventually, but for this project, it's staying as JS.
Here's the updated code, which seems to still be broken. The debug messages aren't appearing at all now. if( (Col.gameObject.name == "$$anonymous$$esh 11") || //Right leg (Col.gameObject.name == "$$anonymous$$esh 12") || (Col.gameObject.name == "$$anonymous$$esh 13")) { print ("Hit right leg"); playerHealth -= armLegImpact; }