- Home /
Health System Help
using System.Collections;
public class HealthSystem : MonoBehaviour
{
public float MaxHealth = 1000.0f;
public float DamageTaken = 0.0f;
public float CurrentHealth = 0.0f;
public GameObject yourguy;
public Collider LeftThigh;
public Collider RightThigh;
public Collider LeftCalve;
public Collider RightCalve;
public Collider LeftUpperArm;
public Collider RightUpperArm;
public Collider LeftForeArm;
public Collider RightForeArm;
public Collider Stomach;
public Collider Chest;
public Collider Neck;
public Collider Head;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
CurrentHealth = MaxHealth - DamageTaken;
if (CurrentHealth <= 0)
{
Debug.Log ("your dead!",yourguy);
}
}
}
Now I cant for the life of me find out to make it so when a specific object hits one of those colliders it does x amount of damage. Also I would want to make it so each collider would have a different amount of damage. Example would be Head takes 1000 damage and RightForeArm takes 200.
Answer by el_kloklo · Mar 10, 2014 at 09:13 PM
Hello,
Maybe you should split up into two script, one for the body part containing its own damage ratio, which call for the parent to update health when its sollicited.
So be simple :
gameobject yourguy, with method take_damage(int damagetaken)
gameobject leftArm, with property damageRatio, and call take_damage of the parent with damageRatio as argument.
Answer by nicolasjr · Mar 10, 2014 at 09:12 PM
Here's what I'd do:
I'd create a class for all this parts, case they're separate game objects, which would go like:
public class BodyPart : MonoBehaviour
{
public event Action<float> onHitted;
public float damage;
public void OnCollisionEnter(Collision p_collision)
{
if(onHitted != null) onHitted(damage);
}
}
After that, I'd change a little bit your main class, and create it like this:
public class HealthSystem : MonoBehaviour
{
public float MaxHealth = 1000.0f;
public float DamageTaken = 0.0f;
public float CurrentHealth = 0.0f;
public List<BodyPart> listBodyPart; //Here you drag from inspector all your bodies part
// Use this for initialization
void Start ()
{
for(int i = 0; i < listBodyPart.Count; i++)
{
listBodyPart[i].onHitted += delegate(float p_damage)
{
DamageTaken += p_damage;
}
}
}
// Update is called once per frame
void Update ()
{
CurrentHealth = MaxHealth - DamageTaken;
if (CurrentHealth <= 0)
{
Debug.Log ("your dead!",yourguy);
}
}
}
Give it a try and let me know how it went!
Sure.
So, if you instantiate a class that has a public event, you'll have access to that event from a class that keeps track of your instances. Once you initialize them, you can handle them as if $$anonymous$$ethods, by using the delegate.
We can say that this:
listBodyPart[i].onHitted += delegate(float p_damage)
{
DamageTaken += p_damage;
}
is a method that receives a float as a parameter and increases the DamageTaken, and it's being "called" from the objects of BodyPart you have in the public list.
So, your logic makes sense: you have a pointer for every collider. That's kind of what you'll have.
However, ins$$anonymous$$d of making the pointers to every collider, which makes the code harder to handle, my suggestion is for you to create a separate class for the game objects that will have the collider (head, arms, etc), and handle the collision in every single one of those instances. In that class, you can use an attribute to keep track of the damage each part causes on the player. That's why there's the public float over there.
This will make the code easier to understand, and, since all the body parts will have the same behavior - only changing the damage value -, it sure is the best way to handle that.
Hope I made myself clear!