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
1
Question by thendricks · Aug 10, 2012 at 12:21 AM · player-health

Need help with player health

I'm creating an fps and I can't find a C# script to make my player lose health when he hits a fireball(enemy bullet). incase you need it heres my healthbar script

 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour 
 {
     public int maxHealth = 100;
     public int curHealth = 100;
 
     public float healthBarLenght;
 
     // Use this for initialization
     void Start () 
     {
         healthBarLenght = Screen.width / 2;
     }
 
     //Update is called once per frame
     void Update() 
     {
         AddJustCurrentHealth(0);
 
     }
 
     void OnGUI()
     {
         GUI.Box(new Rect(10, 10, healthBarLenght, 20), curHealth + "/" + maxHealth);
     }
 
     public void AddJustCurrentHealth(int adj)
     {
        curHealth += adj;
         healthBarLenght = (Screen.width / 2) * (curHealth /(float)maxHealth);
 
     }
 }

and if you can include info about re-spawn

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Daryn · Aug 12, 2012 at 05:54 PM

I would try something along the lines of using a trigger.

so

void OnTriggerEnter () {

      curHealth = curHealth -1;
 }
 // this would make it so when ever it is triggered the curHealth will subtract 1
 then attach a script to the fire ball.
 

void OnTriggerEnter () {

    gameObject.active = false;
 }
 // this will make it disapear when triggered also to make something triggered just check mark the box on the collider component.
 void Update () {

if(curHealth == 0){

Transform.localPosition = new Vector3(x,y,z,),Quaternion.Identifier;

curHealth = maxHealth;

}

}

// this is for the respawn just type in the x y and z coordinates you want the character to respawn at.

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 aldonaletto · Aug 12, 2012 at 02:07 PM

A common approach is to call the function AddjustCurrentHealth in your script when something hits the player. If a fireball collides with the player, you can use something like this in the fireball script (the fireball must be a rigidbody):

void OnCollisionEnter(Collision col){
  // call the function AddJustCurrentHealth(-5) in the target object:
  col.transform.SendMessage("AddJustCurrentHealth", -5, SendMessageOptions.DontRequireReceiver)  
  // destroy the fireball:
  Destroy(gameObject);
}
The instruction SendMessage actually doesn't send anything - it searches in the referenced object's scripts for a function with the specified name and call it (if any).

You should handle respawning in AddJustCurrentHealth: when the health becomes <= 0, play some death effect and decide whether the player can respawn or not (based on life count, for instance). To respawn the player just restore curHealth to maxHealth, move the player to the respawning point and play some respawning effect.
A simple way to add this to your script is as follows:

... // Add these variables: // create an empty object at the respawning point and drag it here: public Transform respawningPoint; public int curLives = 3; // life count public Texture2D blackTexture; // drag a black texture here public float fadeTime = 2; // fade duration float fadeLevel = 0; // fade control: 0 = no fade, 1 = black screen

 void OnGUI()
 {
     GUI.Box(new Rect(10, 10, healthBarLenght, 20), curHealth + "/" + maxHealth);
     // fade the screen to black according to fadeLevel:
     if (fadeLevel > 0){
         GUI.color.a = fadeLevel;
         GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), blackTexture);
     }
 }

 public void AddJustCurrentHealth(int adj)
 {
     curHealth += adj;
     healthBarLenght = (Screen.width / 2) * (curHealth /(float)maxHealth);
     if (curHealth <= 0){
         StartCoroutine(Die());
     }                
 }

 IEnumerator Die(){
     curLives -= 1; // decrement life count
     // death effect: fade screen to black during fadeTime
     while (fadeLevel < 1){
         fadeLevel += Time.deltaTime / fadeTime;
         yield return 0;
     }
     // if player can respawn:
     if (curLives > 0){
         transform.position = respawningPoint.position;
         transform.rotation = respawningPoint.rotation;
         curHealth = maxHealth; // restore health
         // respawn effect: fade screen in
         while (fadeLevel > 0){
             fadeLevel -= Time.deltaTime / fadeTime;
             yield return 0;
         }
     } else {
        // game over code
     }
 }
 

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 thendricks · Aug 13, 2012 at 09:10 PM

thanks for the help

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

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

10 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

Related Questions

Scoring System with Networking 1 Answer

Attach GUI element to newly spawned player as their GUI 2 Answers

Creating 2d Health Bars 2 Answers

How to edit player health based on the player level 2 Answers

How to implement an attack in this script 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