- Home /
I need help with my health script
hi, i was trying to convert these scripts c # to javascript, but they are not working correctly.
playerHealth javascript:
public var maxHealth : int = 100;
public var curHealth : int = 100;
public var healthBarLenght : float;
// Use this for initialization
function Start () {
healthBarLenght = Screen.width / 2;
}
// Update is called once per frame
function Update () {
addjustCurrentHealth(0);
}
function OnGUI () {
GUI.Button(new Rect(10, 40, healthBarLenght, 20), curHealth + "/" + maxHealth);
}
function addjustCurrentHealth(adj : int) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLenght = Screen.width / 2 * curHealth/(maxHealth);
}
playerAttack javascript:
var target : GameObject;
var attackTimer : float;
var coolDown : float;
function Start () {
attackTimer = 0;
coolDown = 0.45f;
}
function Update() {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetButton("Fire1")) {
if(attackTimer == 0)
attackTimer = coolDown;
}
}
private function attack () {
var distance : float = Vector3.Distance(target.transform.position, transform.position);
var dir : Vector3 = (target.transform.position - transform.position).normalized;
var direction : float = Vector3.Dot(dir, transform.forward);
if(distance < 0.8) {
if(direction > 0) {
var eh : enemyVida = (enemyVida).target.GetComponent("enemyVida");
eh.addjustCurrentHealth(-10);
}
}
}
someone could help me.
Answer by Seth-Bergman · Aug 28, 2012 at 06:49 PM
var eh : enemyVida = (enemyVida).target.GetComponent("enemyVida");
change it to
var eh : enemyVida = target.GetComponent(enemyVida);
otherwise this looks ok I think...
but I don't see why you are using GUI.Button, maybe GUI.Label would be more appropriate..
GUI.Label(Rect(10, 40, healthBarLenght, 20), curHealth + "/" + maxHealth);
EDIT:
add this line:
private function attack () {
var distance : float = Vector3.Distance(target.transform.position, transform.position);
Debug.Log(distance);
...etc
also btw, get rid of this:
function Update () {
addjustCurrentHealth(0);
}
it's pointless..
Make sure you are calling the attack function (it is not being called from anything here that I can see).. If the Debug.Log never shows, that's the problem
thanks for the help, but I tried to use your code more failed, does not lower the health.
are you getting any errors? Try adding some debug logs to see if your functions are being entered, and also to see what "distance" is, since it may never be below 0.8. Is the attack function being entered?
just add this line:
private function attack () {
var distance : float = Vector3.Distance(target.transform.position, transform.position);
Debug.Log(distance);
...etc
also btw, get rid of this:
function Update () {
addjustCurrentHealth(0);
}
it's pointless..
Oh, and are you even calling the "attack" function? It doesn't appear so here.. You need to call that function somewhere..
also, is target set to the right object (the one with enemyVida on it)?
Answer by Panik.Studios · Aug 29, 2012 at 12:45 AM
by not working correctly is it that your health always stays at 100? Or that the script has errors.
The way it's written now is saying curhealth = 100;
I would do var curhealth : int; and then after function start() curhealth = 100;
Whats the exact problem? Or, am I reading wrong?