Search Unity

Health Script v2.0 #Free

Discussion in 'Assets and Asset Store' started by GibTreaty, Jun 1, 2013.

  1. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    upload_2019-4-19_3-29-26.png

    Asset Store

    Features - WebGL Player
    - Health Events
    - Kill Assist Events (separate script)
    - Tells you which objects caused the event

    Now using the almighty UnityEvents!
     
    Last edited: Apr 19, 2019
  2. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    382
    please release this would be nice to see more free assets in the store.
     
  3. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    We just have to wait for it to be accepted on the store. Usually takes a few days. I'm happy to give this to everyone. I've been wanting to put a free script on the store for a long time, it's just hard to find the time and the right script to write. This used to be a simple health script but it has changed over the course of a year or two. Then in about a week it evolved into what it is now. I almost didn't release it (thanks to feature-creep). I thought, "Kill assists, that sounds nice I think I'll add that". I wanted to add armor support but that's a touchy subject.
     
  4. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
  5. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    137 downloads in the month of June! Looks like you guys like it. If there's any problems with it or you have any suggestions let me know.
     
  6. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    I am trying to get an object to damage the player as long as the player is in contact with it. I have tried different things but cannot get your script to work. I do not see actual usage instructions just descriptions of the different types of scripts and what they are for. Is there information on what to do to get the scripts to work step by step?
     
  7. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    It's been so long and I'm sorry I missed this question, sorry!

    Basically all you need to do is add the Health component onto the object you want to have health. Then you can make your own script to call Damage and Heal messages (and more) either by using SendMessage or by calling the function directly.

    So for a player and enemy...

    1. Add Health component to player and enemy.
    2. Create a component such as a Weapon script.
    3. In the function that you want to use to do damage you can, for example do..
    Code (csharp):
    1.  
    2. public void Fire(GameObject target, float damage){
    3.   target.SendMessage("Damage", new HealthEvent(gameObject, damageAmount));
    4. }
    5.  
    The HealthEvent stores the GameObject that caused the event, and the amount of damage/restoration that was caused. The change in health is automatically calculated by the Health script and sends out different messages depending on the outcome. For instance if you are at full health and you try to heal, no messages will be sent about a change in health.

    4. To create a script that destroys (or pools the object if you have an object pool), you can add a function that gets the OnDeath event.

    Code (csharp):
    1.  
    2. public void OnDeath(HealthEvent healthEvent){
    3.   if(healthEvent.gameObject)
    4.     Debug.Log(healthEvent.gameObject.name + " has killed " + gameObject.name + " with " + healthEvent.amount + " damage");
    5.  
    6.   Destroy(gameObject);
    7. }
    8.  
    5. Maybe you want your enemy to taunt the player it killed, in that case you can add..

    Code (csharp):
    1.  
    2. public void OnCausedDeath(HealthEvent healthEvent){
    3.   if(healthEvent.gameObject)
    4.     Debug.Log("Mwahaha I have killed " + healthEvent.gameObject.name);
    5. }
    6.  

    Of course you would still need a way to get the target but I'll let you decide that part.
     
  8. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    I'm working on an update for Health Script. For the past few months (years?) I've used it and have since added and modified it. I think the new version is way better than the current one. I've added UnityEvents that replace the messages sent with SendMessage. There a few more events as well and you can toggle whether the health script is invincible and incurable. I've also split the damage assist feature into its own component so you don't have to swap between using a Health and Health Assist script. Here is an image of the current state of the new health script. If the names of the events don't make sense you can suggest a better one.

     
    runningbird likes this.
  9. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    382
    awesome job!
     
    GibTreaty likes this.
  10. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    Health Script v2.0 is ready!

    Health Script 2.0 Release Notes



    Added
    • Moved the health related scripts into the YounGenTech.Health namespace
    • C# Events have been swapped out for UnityEvents. You can now setup events to be triggered using the inspector rather than just through code.
    • Examples of instantiating prefabs and changing UI using the health events
    • OnRestored event in the Health script for when you are fully healed
    • NormalizedValue that will give you a value from 0 to 1 based on a scale from 0 to the max health value

    Changed
    • The assist side of HealthAssist has been seperated for ease of use and turned into the Assist script
    • SendMessage messages are no longer sent from the Health script
    • Health now inherits from the IStat interface for extensibility
    • OnDamaged and OnHealed events now happen even if you were full restored or died within the same call. They are called right before OnDeath and OnRestored
    • And more...
     
    runningbird likes this.
  11. eidolad

    eidolad

    Joined:
    Nov 11, 2015
    Posts:
    1
    Hi, this looks like a useful implementation...nice code style from my 5 minutes of reading.

    A minor minor typo in Health.cs: line 19: "valuue" should be value

    (it shows up in the Unity inspector so it is more apparent than most script typos)


    ...might suggest to run the scripts through a spell checker so nitpicks like me don't find them...
     
    GibTreaty likes this.
  12. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    A typo in my code!? This is blasphemy! I'll check it out ASAP.

    Edit: Thanks for finding that! Downloading a spell checker extension for Visual Studio to make sure there's nothing else wrong and I'll update the package.

    Edit2: The Visual Studio Spell Checker extension is nice. It can go through the entire solution and find typos. Although the only things it found "wrong" were, timestamp, raycast and Tooltip. I'm not changing those because they're technically not wrong haha.

    Edit3: The fix has been submitted!
     
    Last edited: Jan 13, 2016
  13. Pat19645

    Pat19645

    Joined:
    Apr 17, 2014
    Posts:
    14
    Thank you for this nice script!
     
    GibTreaty likes this.
  14. jaycabrera_waconiaschool_unity

    jaycabrera_waconiaschool_unity

    Joined:
    Apr 15, 2019
    Posts:
    5
    Hey! so i'm making a game for my game design class and i don't know how to make the object heal the character on collide. I have the "on collision" part working (I know it's working because i have it delete itself on touch) but I don't know what code to add to make it heal/damage the player. Is there some sort of Documentation or some sort of guide that I can use?
     
  15. jaycabrera_waconiaschool_unity

    jaycabrera_waconiaschool_unity

    Joined:
    Apr 15, 2019
    Posts:
    5
    @GibTreaty
     
  16. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    Sorry for the delay! You simply just need to add the Health component onto the GameObject that you want to have health. Then to cause healing or damage, you need to grab a reference to that health component and call the Heal or Damage functions appropriately.

    The Shoot component has an example of how you can call it.
    upload_2019-4-17_14-33-10.png

    @jaycabrera_waconiaschool_unity
     
    Last edited: Apr 18, 2019
  17. jaycabrera_waconiaschool_unity

    jaycabrera_waconiaschool_unity

    Joined:
    Apr 15, 2019
    Posts:
    5
    Hey! thanks for replying. I'm still a little confused. This is what my script and the errors i'm getting, what am I doing wrong? (Also just wanted to say that i'm new to unity. This is my first time using it, ever.)
     

    Attached Files:

  18. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    @jaycabrera_waconiaschool_unity
    You still need to fill in the parameters like in my example.
    Code (CSharp):
    1. .Damage(new HealthEvent(gameObjectThatCausedDamage, damageAmount));
    2. .Health(new HealthEvent(gameObjectThatCausedHeal, healAmount));
     
  19. jaycabrera_waconiaschool_unity

    jaycabrera_waconiaschool_unity

    Joined:
    Apr 15, 2019
    Posts:
    5
    @GibTreaty
    Hey, sorry if i'm being annoying. but I need your help again. So i want to reset the level on death But i don't know how to do this with you're script. i know "on death" is but I don't know how to reset the level using that. My only other option is to write some code (it would look something like what's in the image) but IDK how to assign "value" from the "health" script to a variable in mine. If you could help me it would be greatly appreciated.

    EDIT: in the image the "<" is supposed to be "="
     

    Attached Files:

  20. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    @jaycabrera_waconiaschool_unity

    One way to do it..
    Code (CSharp):
    1. void Awake() {
    2.     healthComponent.OnDeath.AddListener(ResetLevel);
    3. }
    4.  
    5. void ResetLevel(){
    6.     //do your level reset code here
    7. }
    Also you can check out the YounGen Tech Discord server (I have a link for it in my signature below my posts). I'll be able to help you with things faster there if you want! The only time I'm notified of posts here is when I go check my email... manually... which I don't do super often.
     
  21. jaycabrera_waconiaschool_unity

    jaycabrera_waconiaschool_unity

    Joined:
    Apr 15, 2019
    Posts:
    5
    @GibTreaty
    hey! thanks for helping! I still have some problems. one thing that's important is that I can't join the discord since This project is on a school computer, and We're only allowed to work on it at school. the other problem is that it visual studio says that "healthcomponent" does not exist. Did i do something wrong? (here's my code: https://pastebin.ubuntu.com/p/YvtvRsdbzG/ )
     
    GibTreaty likes this.
  22. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    @jaycabrera_waconiaschool_unity

    I didn't mean for you to only copy my code. You still need to replace "heatlhComponent" with a reference to the actual health as it was just an example.
     
  23. Immo17

    Immo17

    Joined:
    Jul 11, 2019
    Posts:
    1
    Hi any damage script for shadowgun legends game...thank you n included health too...anything email me in kalamfaily8983@gmail.com
     
  24. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    Hmm?