i am wanting to make a health bar that displays on a GUI on the player. this is the enemy script that has access to the health and i require some assistance to make it. I of course want the health bar to go down as the player’s health does.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Chase : MonoBehaviour
{
public AudioSource Cough;
public Transform player;
private Animator anim;
private UnityEngine.AI.NavMeshAgent nav;
public float waitTime = .05f;
public float delay = 3;
public int playerhealth = 50;
// Use this for initialization
void Start()
{
nav = GetComponent<UnityEngine.AI.NavMeshAgent>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(player.position, this.transform.position) < 3.25)
{
StartCoroutine(HarmPlayer());
}
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 80)
{
nav.enabled = false;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.2f);
anim.SetBool("isWalking", false);
if (direction.magnitude > 2)
{
this.transform.Translate(0, 0, 0.15f);
anim.SetBool("Running", true);
anim.SetBool("isAttack", false);
}
else
{
anim.SetBool("isAttack", true);
anim.SetBool("isWalking", false);
}
if ((playerhealth) < 1)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
else
{
nav.enabled = true;
anim.SetBool("isWalking", true);
anim.SetBool("Running", false);
anim.SetBool("isAttack", false);
}
}
IEnumerator HarmPlayer()
{
Cough.Play();
yield return new WaitForSeconds(waitTime);
print("Enemy" + playerhealth);
playerhealth = playerhealth - 1;
}
}