Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

This Is My Health Script I Cant Get The Damage To Work (Need Help Fast!!)

Discussion in 'Scripting' started by Darkdragon0011, May 4, 2015.

  1. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    Code (JavaScript):
    1. static var curHp : float = 100.0;
    2. static var maxHp : float = 50.0;
    3. static var curMana : float = 50.0;
    4. static var maxMana : float = 50.0;
    5. var HpBarTexture : Texture2D;
    6. var ManaBarTexture : Texture2D;
    7. var hpBarLength : float;
    8. var percentOfHp : float;
    9. var manaBarLength : float;
    10. var percentOfMana :float;
    11. var damage : int = 20;
    12. function OnGUI () {
    13.         if (curHp > 0) {
    14.                 GUI.DrawTexture(Rect((Screen.width/2) - 500, 10, hpBarLength, 10), HpBarTexture);
    15.         }
    16.         if (curMana > 0) {
    17.                 GUI.DrawTexture(Rect((Screen.width/2) - 500, 20, manaBarLength, 10), ManaBarTexture);
    18.         }
    19. }
    20. function Update () {
    21.         percentOfHP = curHp/maxHp;
    22.         hpBarLength = percentOfHP*100;
    23.         percentOfMana = curMana/maxMana;
    24.         manaBarLength = percentOfMana*100;
    25.         if(Input.GetKeyDown("h")) {
    26.                 curHp -= 10.0;
    27.         }
    28.         if(Input.GetKeyDown("m")) {
    29.                 curMana -= 10.0;
    30. }
    31. }
    32. function OnCollisionEnter(collision : Collision) {
    33.     if (collision.gameObject.name == "Cube10")
    34.         {
    35.         (curHp) ;curHp - damage;
    36. }
    37. }
    38. //What Do I do For Damage
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    You need to provide more information.

    • What isnt working?
    • Where?
    • What have you already tried?
    Seriously, just saying OMG my script doesnt work then posting and expecting people to fix isnt a good way to get help.
     
    KelsoMRK and dterbeest like this.
  3. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    this part

    * The Damage Collider is now working

    1. function OnCollisionEnter(collision : Collision) {
    2. if (collision.gameObject.name == "Cube10")
    3. {
    4. (curHp) ;curHp - damage;
    5. }
    6. }

      i tried curHp - curHp - damage
      (curHp - curHp = damage)
     
  4. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    curHp = curHp - damage
     
  5. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
  6. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    I just Started Unity thats why i ask a lot of questions
     
  7. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    One More Question is there a way i can slow the damage because it just drains my health
     
  8. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Are there lots of "cube10" game objects? OnCollisionEnter will only be called once per cube.
     
  9. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    No Just One I Plan on organizing my game more today
     
  10. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    You can also just

    curHp -= damage

    It does the exact same thing than
    curHp = curHp - damage
     
  11. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    A couple other observations...:

    Get rid of the static on your health and mana now... it may be slightly easier for you to access elsewhere but you'll find its just as easy to use get component. Static will just lead you to the usual "why do all my enemies die when one does" question later on. Static for health will lead to trouble unless you intend only ONE object that will ever have something called "curHP".

    I would believe that most games with collision style damage will either have a small invincibility time after taking damage or on damage force the 2 objects apart to avoid massive damage on what seems a single hit.
     
  12. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    ok i will try
     
  13. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    thanks a lot now all i need is a meele system it get confusing sometimes
     
  14. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    So I made a Whole New code with all you guys recommendations in it i think its good,

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var curHp : float = 100.0;
    4. var maxHp : int = 100;
    5.  
    6. var currentThirst : float = 100.0;
    7. var maxThirst : int = 100;
    8.  
    9. var currentHunger : float = 100.0;
    10. var maxHunger : int = 100;
    11. var lastPositionY : float = 0f;
    12. var fallDistance : float = 0f;
    13. var player : Transform;
    14. private var controller : CharacterController;
    15. private var barLength = 0.0;
    16.  
    17. function Start()
    18. {
    19.     barLength = Screen.width / 8;
    20. }
    21.  
    22. function Update()
    23. {
    24.     if(curHp <= 0)
    25.     {
    26.         CharacterDeath();
    27.     }
    28.    
    29.     /* THIRST CONTROL SECTION*/
    30.    
    31.     //Normal thirst degredation
    32.     if(currentThirst >= 0)
    33.     {
    34.         currentThirst -= Time.deltaTime / 5;
    35.     }
    36.    
    37.     if(currentThirst <= 0)
    38.     {
    39.         currentThirst = 0;
    40.     }
    41.    
    42.     if(currentThirst >= maxThirst)
    43.     {
    44.         currentThirst = maxThirst;
    45.     }
    46.    
    47.     /* HUNGER CONTROL SECTION*/
    48.         if(currentHunger >= 0)
    49.     {
    50.         currentHunger -= Time.deltaTime / 8;
    51.     }
    52.    
    53.     if(currentHunger <= 0)
    54.     {
    55.         currentHunger = 0;
    56.     }
    57.    
    58.     if(currentHunger >= maxHunger)
    59.     {
    60.         currentHunger = maxHunger;
    61.     }
    62.    
    63.     /* DAMAGE CONTROL SECTION*/
    64.     if(currentHunger <= 0 && (currentThirst <= 0))
    65.     {
    66.         currentHealth -= Time.deltaTime / 4;
    67.         if (currentHunger <= 0){
    68.        
    69.             print ("You Died"); }
    70.     }
    71.    
    72.     else
    73.     {
    74.         if(currentHunger <= 0 || currentThirst <= 0)
    75.         {
    76.             curHp -= Time.deltaTime / 8;
    77.         }
    78.     }
    79. }
    80.  
    81. function CharacterDeath()
    82. {
    83.     Application.LoadLevel("SimpleMenu");
    84. }
    85.  
    86. function OnGUI()
    87. {
    88.     //Icons
    89.     GUI.Box(new Rect(5, 30, 50, 23), "Health");
    90.     GUI.Box(new Rect(5, 55, 50, 23), "Thirst");
    91.     GUI.Box(new Rect(5, 80, 50, 23), "Hunger");
    92.    
    93.     //Health / Hunger / Thirst bars
    94.     GUI.Box(new Rect(55, 30, barLength, 23), currentHealth.ToString("0") + "/" + maxHealth);
    95.     GUI.Box(new Rect(55, 55, barLength, 23), currentThirst.ToString("0") + "/" + maxThirst);
    96.     GUI.Box(new Rect(55, 80, barLength, 23), currentHunger.ToString("0") + "/" + maxHunger);
    97.    
    98. }
    99.  
    100. function OnTriggerEnter (other : Collider) {
    101.  
    102.     if(other.gameObject == ("CocconutCol") && currentHunger <= 90.0){
    103.    
    104.         currentHunger += 10;
    105.         Destroy (other.gameObject.transform.parent.gameObject);
    106.  
    107. }
    108. }
    109.  
    110. function OnControllerColliderHit(hit : ControllerColliderHit){
    111.      if (hit.gameObject.CompareTag("Enemy")){
    112.    
    113.          curHp = curHp - 5;
    114.      }
    115. }
     
  15. Witcher_CA

    Witcher_CA

    Joined:
    May 5, 2015
    Posts:
    19
    Dark's code works perfectly!
     
  16. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    yea but my health gets drained i need time were he can't hit me
     
  17. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    at the top add

    Code (csharp):
    1.  
    2. var invincibleTime : float = 2.0f; // 2 sec... change to whatever
    3. var nextHitTime : float = -1.0f; // just a start with a low value
    4.  
    and in
    Code (csharp):
    1.  
    2. function OnControllerColliderHit(hit : ControllerColliderHit){
    3.      if (hit.gameObject.CompareTag("Enemy")) {
    4.            if(Time.time >= nextHitTime){
    5.                 curHp = curHp - 5;
    6.                 nextHitTime = Time.time + invincibleTime;
    7.      }
    8. }
    9.  
     
  18. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    Thanks It Works
     
  19. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    Now I'm gunna set up a enemy health system
     
  20. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    You'll get better results if you use C# as you can use the new GUI which is tons easier to set up!