• 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
Question by theok_1989 · Apr 03, 2013 at 01:16 PM · damagedeathdiedeadhealth

Player Health Damage

Hi guys, I have this health script I created for my player, can any one show me how I could integrate damage and death when the player comes close to an enemy? This is the only thing not working in my game and I have a project completion deadline of the 3rd May, so any kind of detailed help would be gratefully appreciated. Thanks in advance!

var maxHealth : int = 100; var HealthGUI : GUIStyle;

var rect1: float; var rect2: float; var rect3: float; var rect4: float;

function Update () {

 if( maxHealth < 0){
 
   maxHealth = 0;
 
 }
 
 
 if( maxHealth > 100){
 
  maxHealth = 100;
 
 }
 
 

}

function OnGUI (){

  GUI.Label(new Rect(Screen.width/rect1,Screen.height/rect2, Screen.width/rect3,Screen.height/rect4 ),""+maxHealth+"%",HealthGUI);

}

Comment

People who like this

0 Show 0
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

Answer by LyanApps · Apr 03, 2013 at 01:58 PM

You can add a Sphere collider to an enemy object, let's say 5 units large. Set the collider to istrigger. When your player (which should also have a collider, usually a capsule collider) touches the sphere collider, the OnTriggerEnter Event is called. http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

From here you can call a function on your player object such as:

 function takesDamage(amount){
     maxHealth -= amount;
 }

Then when you check if(maxHealth<0) you know your player has died. Here you can add effects but ultimately you want to call Destroy(player). http://docs.unity3d.com/Documentation//ScriptReference/Object.Destroy.html

Comment

People who like this

0 Show 0 · 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

Answer by Yokimato · Apr 03, 2013 at 02:08 PM

I prefer using OnCollisionEnter for detecting hits for combat since it's very precise (at a performance cost). If you're not so concerned about accuracy, LyanApps's advise to use triggers would certainly do the trick.

However, to your question, after a collision is detected (via either method described), you need to alert the hit object that a collision occurred.

I use something similar to this:

 void OnCollisionEnter(Collision collision) {
     ContactPoint contact = collision.contacts[0];
     GameObject other = contact.otherCollider.gameObject;
     float someRandomDamageValue = 20.5f;
     other.SendMessage("OnHit", someRandomDamageValue ,SendMessageOptions.DontRequireReceiver);
 }

Your player (or anything that needs to get hit) can have a component (I call mine 'HealthController') attached, that is in charge of health and has the "OnHit" function. That function then is called on the object that was hit! Here's an example of the HealthController:

 public class HealthController : MonoBehaviour {
    public float HP;
    public float MaxHP;
 
    public void OnHit(float damage) {
       HP -= damage;
       HP = Mathf.Clamp(HP, 0f, MaxHP);
 
       if(HP == 0) {
         BeginDeathSequence();
       }
    }
 
    private void BeginDeathSequence() {
       // Oh Noes! We Died!
    }
 
 }

Works really nicely, and it's a clean way to communicate the collision and damage.

Comment

People who like this

0 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 hike1 · Apr 03, 2013 at 02:30 PM 0
Share

Yokimato, that looks like melee damage only, what about projectile damage?

avatar image Yokimato · Apr 03, 2013 at 03:27 PM 0
Share

I think it depends on how your projectiles work. If they're still collision-based, the collision detection works as-is. If it's raycast-base (like a bullet or something), if the raycast hits, send the hit object the same message. The beauty is, is that if it has a health controller, it will handle it, and if it doesn't, no harm no foul.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Player taking damage on collision. Can't get the script to work!? 1 Answer

How to make death messages show at random? 1 Answer

bullet damage problem 1 Answer

how to take health/hearth from player when colliding with an object? 2 Answers

Scene not restarting after player death 2 Answers


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