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

C# Health Bar please help

Discussion in 'Scripting' started by Aymxne, Dec 27, 2015.

  1. Aymxne

    Aymxne

    Joined:
    Dec 26, 2015
    Posts:
    34
    Hello,

    I am currently trying to create a health bar which i managed to do, however every time i touch the wall all of the health goes which is bad. What i want it to do is to take 10 hits to the ball before it becomes destroyed, if anyone got a script that i can follow or an example that would be great.

    I hope you can follow my code as i am new to this :)

    thank you
    here is the code


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6.  
    7. public class Controlles : MonoBehaviour {
    8.  
    9.  
    10.     //health bar section
    11.     public Slider healthBar;
    12.    
    13.     //speed for the ball
    14.     public float speed;
    15.     //lets try the jump ability agian
    16.     private bool onGround;
    17.     private float minJump;
    18.     private float maxJump;
    19.     //this if for the cool down
    20.     public float coolDown = 5f;
    21.     public float coolDownTimer;
    22.     public bool JumpCoolDown = false;
    23.    
    24.  
    25.    
    26.     public Text countText;
    27.     public Text winText;
    28.    
    29.     private Rigidbody rb;
    30.     private int count;
    31.  
    32.  
    33.     void Start()
    34.     {
    35.         //health
    36.         healthBar.value = 100;
    37.        
    38.         //when i start the game i can jump stright away
    39.         onGround = true;
    40.         minJump = 2f;
    41.         maxJump = 8f;
    42.         rb = GetComponent<Rigidbody>();
    43.         count = 0;
    44.         SetCountText ();
    45.         winText.text = "";
    46.  
    47.  
    48.         transform.position = new Vector3(0, 3, 0);
    49.     }
    50.  
    51.  
    52.     void FixedUpdate ()
    53.     {
    54.        
    55.        
    56.         //cool down
    57.         if (coolDownTimer > 0)
    58.         {
    59.             coolDownTimer -= Time.deltaTime;
    60.  
    61.         }
    62.        
    63.         if (coolDownTimer < 0)
    64.         {
    65.             coolDownTimer = 0;
    66.         }
    67.  
    68.         if (Input.GetButton("Jump") && coolDownTimer == 0)
    69.         {
    70.             Debug.Log("Hi!");
    71.             coolDownTimer = coolDown;
    72.         }
    73.         if(onGround)
    74.         {
    75.             if(Input.GetButton("Jump"))
    76.             {
    77.                 if(minJump < maxJump)
    78.                 {
    79.                     minJump += Time.deltaTime * 5f;
    80.                 }
    81.  
    82.                 else
    83.                 {
    84.                     minJump = maxJump;
    85.                 }
    86.  
    87.                 print(minJump);
    88.             }
    89.  
    90.             else
    91.             {
    92.                 if (minJump > 0f)
    93.                 {
    94.                     minJump = minJump + maxJump;
    95.                     rb.velocity = new Vector3(0f, maxJump, 0f);
    96.                     minJump = 0f;
    97.                     onGround = false;
    98.                 }
    99.             }
    100.         }
    101.  
    102.         float moveHorizontal = Input.GetAxis("Horizontal");
    103.         float moveVertical = Input.GetAxis("Vertical");
    104.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    105.         rb.AddForce(movement * speed);
    106.        
    107.  
    108.  
    109.        
    110.     }
    111.     /*void OnTriggerStay(Collision col)
    112.     {
    113.         if(col.gameObject.tag == "Wall")
    114.         {
    115.             healthBar.value -= 10;
    116.         }
    117.     }*/
    118.     /*void OnCollisionEnter(Collision col)
    119.     {
    120.         if(col.gameObject.CompareTag("Wall"))
    121.         {
    122.             health -= 10;
    123.         }
    124.  
    125.         if(health <= 0)
    126.         {
    127.             Destroy(gameObject);
    128.         }
    129.     }*/
    130.     void OnCollisionEnter(Collision other)
    131.     {
    132.         //this is when i touch the ground i am able to jump but i will add the 5 second delay lyaer
    133.         if (other.gameObject.CompareTag("ground"))
    134.         {
    135.             onGround = true;
    136.         }
    137.  
    138.        //heath section
    139.         if(other.gameObject.CompareTag("Wall"))
    140.         {
    141.             healthBar.value -= 1;
    142.         }
    143.         if(healthBar.value <= 0)
    144.         {
    145.             Destroy(gameObject);
    146.         }
    147.  
    148.     }
    149.  
    150.     void OnTriggerEnter(Collider other)
    151.     {
    152.         if (other.gameObject.CompareTag("Pick Up"))
    153.         {
    154.             other.gameObject.SetActive(false);
    155.             count = count + 1;
    156.             SetCountText ();
    157.         }
    158.  
    159.        
    160.     }
    161.     void SetCountText ()
    162.     {
    163.         countText.text = "Count  " + count.ToString ();
    164.         if (count >= 12)
    165.         {
    166.             winText.text = "You Win!";
    167.         }
    168.     }
    169. }
     
  2. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    try this:
    Code (CSharp):
    1. void OnCollisionEnter(Collision other)
    2.     {
    3.         //this is when i touch the ground i am able to jump but i will add the 5 second delay lyaer
    4.         if (other.gameObject.CompareTag("ground"))
    5.         {
    6.             onGround = true;
    7.         }
    8.        //heath section
    9.         if(other.gameObject.CompareTag("Wall"))
    10.         {
    11.             healthBar.value -= 0.1f;
    12.         }
    13.         if(healthBar.value <= 0)
    14.         {
    15.             Destroy(gameObject);
    16.         }
    17.     }
     
    Aymxne likes this.
  3. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Not sure if you have this but make sure you set the slider's max value to 100 before decrementing the value. Otherwise you're just subtracting 1 from 1.
     
    Aymxne likes this.
  4. Aymxne

    Aymxne

    Joined:
    Dec 26, 2015
    Posts:
    34
    Thank you guys i have got it working i appreciate all your help :)