Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

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:
    769
    [​IMG]

    Asset Store

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

    Now using the almighty UnityEvents!

    [​IMG]
     
    Last edited: Dec 22, 2015
  2. runningbird

    runningbird

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

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    769
    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:
    769
  5. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    769
    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:
    39
    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:
    769
    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:
    769
    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.

    [​IMG]
     
    runningbird likes this.
  9. runningbird

    runningbird

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

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    769
    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:
    769
    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:
    13
    Thank you for this nice script!
     
    GibTreaty likes this.