I’m currently making a health bar for my 2D shooter. However, I’m not using any sliders.
Using transform.localScale, I was able to make the healthbar move when I was playing with Input.GetKey.
However, my code doesn’t work when it detects damage. I’m not really sure.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class health : MonoBehaviour {
//hp is health in our case
public float hp = 100f;
public GameObject explode;
private int number_of_deaths;
//this is the scoreboard!
public Text ScoreBoard = null;
private float float_add;
private bool stop_decrese;
public Text dispaly_health;
public float rate = -0.001f;
// Use this for initialization
void Start () {
//we initlize the number of deaths at the beginning of the game
number_of_deaths = 0;
stop_decrese = true;
}
// Update is called once per frame
void Update () {
}
void ApplyDmg(float d){
/*
hp = hp - d;
*/
if (stop_decrese == true)
{
hp = hp - 1;
dispaly_health.text = hp.ToString();
transform.position = new Vector3(transform.position.x - rate, transform.position.y, transform.position.z);
float_add = float_add + rate;
transform.localScale += new Vector3(float_add, 0f, 0f);
print(hp);
}
if (transform.localScale.x < 0.02f)
{
hp = 0;
dispaly_health.text = hp.ToString();
stop_decrese = false;
print("Past!!!");
}
if (hp <= 0)
{
Instantiate(explode, new Vector3(transform.position.x, transform.position.y, 0f), Quaternion.identity);
Destroy(this.gameObject);
}
}
}