Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrAnti · Mar 10, 2014 at 08:04 PM · physicstriggerhealth

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrAnti · Mar 11, 2014 at 03:09 AM 0
Share

Could you explain it a little more?

avatar image nicolasjr · Mar 11, 2014 at 12:19 PM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to deny an exit (and therefore OnTriggerExit) 2 Answers

Collider/trigger collision causing physics glitch 0 Answers

overlapsphere to destroy NPCs on exit 1 Answer

Collider Lifts me off Ground ??? And I'm afraid of heights !!! 1 Answer

Prevent shooting when gun is inside wall 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges