Hey guys…
I wonder if anyone could help me writing this in C#, instead of Java…
It is wokring fine in Unity, but when I try to compile it for the Metro Store, it gives me an error so I want to have it in C# so I can have everything under the same folder.
This is the JS script:
var hp : float;
var maxHP : float;
var healthBarWidth : int;
var myHealthBar : GameObject;
var myhb : GameObject;
var health : BossCB;
function Awake(){
health = this.GetComponent("BossCB");
}
function Start () {
healthBarWidth = 20;
myhb=Instantiate(myHealthBar,transform.position, transform.rotation);
maxHP = 300;
hp = maxHP;
}
function Update () {
myhb.transform.position = Camera.main.WorldToViewportPoint (transform.position);
myhb.transform.position.x -= .05;
myhb.transform.position.y -= .05;
myhb.transform.localScale=Vector3.zero;
hp = health._healths;
var healthPercent : float = hp/maxHP;
if(healthPercent<0){healthPercent=0;}
if(healthPercent>100){healthPercent=100;}
healthBarWidth=healthPercent*20;
myhb.guiTexture.pixelInset=Rect(10,10,healthBarWidth,5);
}
Thanks!!
I have been translating some and I have this, so far:
using UnityEngine;
using System.Collections;
public class PlanetBossHealthBarC : MonoBehaviour {
public float hp;
public float maxHP;
public int healthBarWidth;
public GameObject myHealthBar;
public GameObject myhb;
private PlanetBoosCB health;
public bool hbCreation=true;
void Awake()
{
health = GetComponent<PlanetBoosCB>();
}
void Start()
{
healthBarWidth = 20;
myhb = Instantiate(myHealthBar, transform.position, transform.rotation)as GameObject;
maxHP = 3000;
hp = maxHP;
}
void Update()
{
hp = health._healths;
float healthPercent = hp/maxHP;
if(healthPercent<0){healthPercent=0;}
if(healthPercent>100){healthPercent=100;}
healthBarWidth=(int)healthPercent*20;
myHealthBar.guiTexture.pixelInset = new Rect(50, 50, healthBarWidth, 5);
}
}
So what I am missing is:
myhb.transform.position = Camera.main.WorldToViewportPoint (transform.position);
myhb.transform.position.x -= .05;
myhb.transform.position.y -= .05;
myhb.transform.localScale=Vector3.zero;
The health bar suppose to appear in the middle of its parent… but there´s nothing showing up yet
Any ideas?
Your transform.localScale is set to 0, try setting it to 1 and see if that fixes it.
Having this
using UnityEngine;
using System.Collections;
public class PlanetBossHealthBarC : MonoBehaviour {
public float hp;
public float maxHP;
public float healthBarWidth;
public GameObject myHealthBar;
public GameObject myhb;
private PlanetBoosCB health;
public bool hbCreation=true;
void Awake()
{
health = GetComponent<PlanetBoosCB>();
}
void Start()
{
healthBarWidth = 20;
myhb = Instantiate(myHealthBar, transform.position, transform.rotation)as GameObject;
maxHP = 3000;
hp = maxHP;
}
void Update()
{
myhb.transform.position = Camera.main.WorldToViewportPoint(transform.position);
// myhb.transform.position.x -= .05;
// myhb.transform.position.y -= .05;
myhb.transform.localScale = Vector3.zero;
hp = health._healths;
float healthPercent = hp/maxHP;
if(healthPercent<0){healthPercent=0;}
if(healthPercent>100){healthPercent=100;}
healthBarWidth=healthPercent*20;
myHealthBar.guiTexture.pixelInset = new Rect(50, 50, healthBarWidth, 5);
}
}
I have the little bar a little bit to the side of its parent… but it is not decreasing while its parent gets hit (as it suppose to happen)
I have been debugging… and I found:
Everything is decreasing each time the parent gets hit, which is right: hp, healthBarWidth and healthPercent.
However:
a) The bar does not change dinamically
b) depending on what I debug, I get different bar sizes:
hp = full bar
healthBarWidth = 20
healthPercent = 1
How do I make it work the way it suppose to?
Found it… my bad.
Last line has to be myhb instead of myHealthBar 