hello all,
i’m currently learning how to use unity 3d by following BurgZergArcade’s rpg tutorial. Right now i have the little cube enemies approaching and attacking the player, however, it looks like the player has two health bar boxes layered on top of each other (i apologize if i didn’t get the terminology correct)
my C# script for the player health
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AdjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 10, Screen.width / 2 / (maxHealth / curHealth), 20), curHealth + "/" + maxHealth);
}
public void AdjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
my C# script for enemies attacking:
using UnityEngine;
using System.Collections;
public class EnemyAttack : MonoBehaviour {
public GameObject target;
public float AttackTimer;
public float CoolDown;
// Use this for initialization
void Start () {
AttackTimer = 0;
CoolDown = 2.0f;
}
// Update is called once per frame
void Update () {
if(AttackTimer > 0)
AttackTimer -= Time.deltaTime;
if(AttackTimer < 0)
AttackTimer = 0;
if(AttackTimer == 0){
Attack();
AttackTimer = CoolDown;
}
}
private void Attack(){
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position -transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 2.5f) {
if(direction > 0) {
PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
eh.AdjustCurrentHealth(-10);
}
}
}
}
i have a feeling that it has to do with that last bit of code
PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
eh.AdjustCurrentHealth(-10);
but i’m not too sure.
i tried looking through the forums (and continue lurking), however it didn’t seem like anyone else is having this problem, and i paused the video and checked the codes line by line to see if i did something wrong, but it seemed like i copied it perfectly. >_>
thanks for all the help!