I have been trying to implement a health bar that decreases when my player is damaged, though no matter which method I use, the image doesn’t scale down, it stays the same.
These are the tutorials I have tried and none have worked:
3. Unity 5 health bar tutorial - Values - YouTube
Unity 5 2D Platformer Tutorial - Part 13 - Player Health UI - YouTubeThis is the current code I am using:
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public float Health = 100f;
private SpriteRenderer healthbar;
private Vector3 healthscale;
void start(){
healthbar = GameObject.Find ("HealthBar").GetComponent<SpriteRenderer> ();
healthscale = healthbar.transform.localScale;
}
void update(){
if (Health <= 0) {
Application.Quit ();
Debug.Log ("The game should quit right now.");
}
if (Health > 100) {
Health = 100;
}
HealthUpdate ();
}
void HealthUpdate(){
healthbar.transform.localScale = new Vector3 (healthscale.x * Health * 0.01f, 1, 1);
}
}