Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with my health bar script

Discussion in 'Scripting' started by Frosty96, Aug 28, 2014.

  1. Frosty96

    Frosty96

    Joined:
    Mar 24, 2013
    Posts:
    8
    Hello everyone, i'm following a small tutorial that covers basics but not too much detail. I have a script here that has a working health bar, water/food, and stamina system. My question is does anyone know how I could make my health bar go down from getting hit by enemies rather than just dying of starvation or thirst. Here's the script, all help is appreciated.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. //Size of Textures
    4.  
    5. var size : Vector2 = new Vector2(240, 40);
    6.  
    7. //Health Variables
    8. var healthPos : Vector2 = new Vector2(20, 20);
    9. var healthBarDisplay : float = 1;
    10. var healthBarEmpty : Texture2D;
    11. var healthBarFull : Texture2D;
    12.  
    13. //Hunger Variables
    14. var hungerPos : Vector2 = new Vector2(20, 60);
    15. var hungerBarDisplay : float = 1;
    16. var hungerBarEmpty : Texture2D;
    17. var hungerBarFull : Texture2D;
    18.  
    19. //Thirst Variables
    20. var thirstPos : Vector2 = new Vector2(20, 100);
    21. var thirstBarDisplay : float = 1;
    22. var thirstBarEmpty : Texture2D;
    23. var thirstBarFull : Texture2D;
    24.  
    25. //Stamina Variables
    26. var staminaPos : Vector2 = new Vector2(20, 140);
    27. var staminaBarDisplay : float = 1;
    28. var staminaBarEmpty : Texture2D;
    29. var staminaBarFull : Texture2D;
    30.  
    31. //Fall Rate
    32. var healthFallRate : int = 150;
    33. var hungerFallRate : int = 200;
    34. var thirstFallRate : int = 200;
    35. var staminaFallRate : int = 40;
    36.  
    37. private var chMotor : CharacterMotor;
    38. private var controller : CharacterController;
    39.  
    40. var canJump : boolean = false;
    41.  
    42. var jumpTimer : float = 0.7;
    43.  
    44. function Start()
    45. {
    46.     chMotor = GetComponent(CharacterMotor);
    47.     controller = GetComponent(CharacterController);
    48. }
    49.  
    50. function OnGUI()
    51. {
    52.     //Health GUI
    53.     GUI.BeginGroup(new Rect (healthPos.x, healthPos.y, size.x, size.y));
    54.     GUI.Box(Rect(0, 0, size.x, size.y), healthBarEmpty);
    55.  
    56.     GUI.BeginGroup(new Rect (0, 0, size.x * healthBarDisplay, size.y));
    57.     GUI.Box(Rect(0, 0, size.x, size.y), healthBarFull);
    58.  
    59.     GUI.EndGroup();
    60.     GUI.EndGroup();
    61.  
    62.     //Hunger GUI
    63.     GUI.BeginGroup(new Rect (hungerPos.x, hungerPos.y, size.x, size.y));
    64.     GUI.Box(Rect(0, 0, size.x, size.y), hungerBarEmpty);
    65.  
    66.     GUI.BeginGroup(new Rect (0, 0, size.x * hungerBarDisplay, size.y));
    67.     GUI.Box(Rect(0, 0, size.x, size.y), hungerBarFull);
    68.  
    69.     GUI.EndGroup();
    70.     GUI.EndGroup();
    71.  
    72.     //Thirst GUI
    73.     GUI.BeginGroup(new Rect (thirstPos.x, thirstPos.y, size.x, size.y));
    74.     GUI.Box(Rect(0, 0, size.x, size.y), thirstBarEmpty);
    75.  
    76.     GUI.BeginGroup(new Rect (0, 0, size.x * thirstBarDisplay, size.y));
    77.     GUI.Box(Rect(0, 0, size.x, size.y), thirstBarFull);
    78.  
    79.     GUI.EndGroup();
    80.     GUI.EndGroup();
    81.  
    82.     //Stamina GUI
    83.     GUI.BeginGroup(new Rect (staminaPos.x, staminaPos.y, size.x, size.y));
    84.     GUI.Box(Rect(0, 0, size.x, size.y), staminaBarEmpty);
    85.  
    86.     GUI.BeginGroup(new Rect (0, 0, size.x * staminaBarDisplay, size.y));
    87.     GUI.Box(Rect(0, 0, size.x, size.y), staminaBarFull);
    88.  
    89.     GUI.EndGroup();
    90.     GUI.EndGroup();
    91. }
    92.  
    93. function Update()
    94. {
    95.     //HEALTH CONTROL SECTION
    96.     if(hungerBarDisplay <= 0 && (thirstBarDisplay <= 0))
    97.     {
    98.         healthBarDisplay -= Time.deltaTime / healthFallRate * 2;
    99.     }
    100.  
    101.     else
    102.     {
    103.         if(hungerBarDisplay <= 0 || thirstBarDisplay <= 0)
    104.         {
    105.             healthBarDisplay -= Time.deltaTime / healthFallRate;
    106.         }
    107.     }
    108.  
    109.     if(healthBarDisplay <= 0)
    110.     {
    111.         CharacterDeath();
    112.     }
    113.  
    114.     //HUNGER CONTROL SECTION
    115.     if(hungerBarDisplay >= 0)
    116.     {
    117.         hungerBarDisplay -= Time.deltaTime / hungerFallRate;
    118.     }
    119.  
    120.     if(hungerBarDisplay <= 0)
    121.     {
    122.         hungerBarDisplay = 0;
    123.     }
    124.  
    125.     if(hungerBarDisplay >= 1)
    126.     {
    127.         hungerBarDisplay = 1;
    128.     }
    129.  
    130.     //THIRST CONTROL SECTION
    131.     if(thirstBarDisplay >= 0)
    132.     {
    133.         thirstBarDisplay -= Time.deltaTime / thirstFallRate;
    134.     }
    135.  
    136.     if(thirstBarDisplay <= 0)
    137.     {
    138.         thirstBarDisplay = 0;
    139.     }
    140.  
    141.     if(thirstBarDisplay >= 1)
    142.     {
    143.         thirstBarDisplay = 1;
    144.     }
    145.  
    146.     //STAMINA CONTROL SECTION
    147.     if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
    148.     {
    149.         chMotor.movement.maxForwardSpeed = 15;
    150.         chMotor.movement.maxSidewaysSpeed = 15;
    151.         staminaBarDisplay -= Time.deltaTime / staminaFallRate;
    152.     }
    153.  
    154.     else
    155.     {
    156.         chMotor.movement.maxForwardSpeed = 10;
    157.         chMotor.movement.maxSidewaysSpeed = 10;
    158.         staminaBarDisplay += Time.deltaTime / staminaFallRate;
    159.     }
    160.  
    161.     //JUMPING SECTION
    162.     if(Input.GetKeyDown(KeyCode.Space) && canJump == true)
    163.     {
    164.         staminaBarDisplay -= 0.04;
    165.         Wait();
    166.     }
    167.  
    168.     if(canJump == false)
    169.     {
    170.         jumpTimer -= Time.deltaTime;
    171.         chMotor.jumping.enabled = false;
    172.     }
    173.  
    174.     if(jumpTimer <= 0)
    175.     {
    176.         canJump = true;
    177.         chMotor.jumping.enabled = true;
    178.         jumpTimer = 0.7;
    179.     }
    180.  
    181.     //COMMENTED THESE SECTIONS OUT - UPDATED 16/07/14
    182.     //if(staminaBarDisplay <= 0.05)
    183.     //{
    184.         //canJump = false;
    185.         //chMotor.jumping.enabled = false;
    186.     //}
    187.  
    188.     //else
    189.     //{
    190.         //canJump = true;
    191.         //chMotor.jumping.enabled = true;
    192.     //}
    193.  
    194.     if(staminaBarDisplay >= 1)
    195.     {
    196.         staminaBarDisplay = 1;
    197.     }
    198.  
    199.     if(staminaBarDisplay <= 0)
    200.     {
    201.         //ADDED line 181 here!
    202.         staminaBarDisplay = 0;
    203.         canJump = false;
    204.         chMotor.jumping.enabled = false;
    205.         chMotor.movement.maxForwardSpeed = 6;
    206.         chMotor.movement.maxSidewaysSpeed = 6;
    207.     }
    208. }
    209.  
    210. function CharacterDeath()
    211. {
    212.     Application.LoadLevel("SIMPLELEVEL");
    213. }
    214.  
    215. function Wait()
    216. {
    217.     yield WaitForSeconds(0.1);
    218.     canJump = false;
    219. }
     
    Last edited: Aug 28, 2014
  2. Frosty96

    Frosty96

    Joined:
    Mar 24, 2013
    Posts:
    8
    I should probably add this is my enemy damage script
    Code (JavaScript):
    1. //IMPORTANT NOTE! THIS SCRIPT WAS MADE IN VIDEO NUMBER 21! CHECK OUT THE EARLIER VERSION (NOT V2) IF YOU HAVEN'T REACHED THAT VIDEO.
    2.  
    3. var Distance;
    4. var Target : Transform;
    5. var lookAtDistance = 25.0;
    6. var chaseRange = 15.0;
    7. var attackRange = 1.5;
    8. var moveSpeed = 5.0;
    9. var Damping = 6.0;
    10. var attackRepeatTime = 1;
    11.  
    12. var TheDammage = 40;
    13.  
    14. private var attackTime : float;
    15.  
    16. var controller : CharacterController;
    17. var gravity : float = 20.0;
    18. private var MoveDirection : Vector3 = Vector3.zero;
    19.  
    20. function Start ()
    21. {
    22.     attackTime = Time.time;
    23. }
    24.  
    25. function Update ()
    26. {
    27.     if(RespawnMenuV2.playerIsDead == false)
    28.     {
    29.         Distance = Vector3.Distance(Target.position, transform.position);
    30.        
    31.         if (Distance < lookAtDistance)
    32.         {
    33.             lookAt();
    34.         }
    35.        
    36.         if (Distance > lookAtDistance)
    37.         {
    38.             renderer.material.color = Color.green;
    39.         }
    40.        
    41.         if (Distance < attackRange)
    42.         {
    43.             attack();
    44.         }
    45.         else if (Distance < chaseRange)
    46.         {
    47.             chase ();
    48.         }
    49.     }
    50. }
    51.  
    52. function lookAt ()
    53. {
    54.     renderer.material.color = Color.yellow;
    55.     var rotation = Quaternion.LookRotation(Target.position - transform.position);
    56.     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
    57. }
    58.  
    59. function chase ()
    60. {
    61.     renderer.material.color = Color.red;
    62.    
    63.     moveDirection = transform.forward;
    64.     moveDirection *= moveSpeed;
    65.    
    66.     moveDirection.y -= gravity * Time.deltaTime;
    67.     controller.Move(moveDirection * Time.deltaTime);
    68. }
    69.  
    70. function attack ()
    71. {
    72.     if (Time.time > attackTime)
    73.     {
    74.         Target.SendMessage("ApplyDammage", TheDammage);
    75.         Debug.Log("The Enemy Has Attacked");
    76.         attackTime = Time.time + attackRepeatTime;
    77.     }
    78. }
    79.  
    80. function ApplyDammage ()
    81. {
    82.     chaseRange += 30;
    83.     moveSpeed += 2;
    84.     lookAtDistance += 40;
    85. }