- Home /
Health Bar Alignment Issue
Hello,
I have a health bar system on my enemy which is a game object plane that simply decreases in size according to the health variable. The only slight problem I'm having is that the plane decreases on both sides, where as I only want it to decrease on the right side. So its alignment is always left.
Here is a video to get a better understanding: http://www.youtube.com/watch?v=qZ3K36wlTc4
So what I was thinking is maybe I could reposition the plane every time the health variable goes down:
// This changes the size of plane: HealthBar.transform.localScale.x = MaxBar * (Health/MaxHealth);
// This to be used for alignment? \\ HealthBar.localPosition = Vector3((keep aligned left), 60, -5);
// MaxBar = Size of the plane in 'X' // MaxHealth = Total health // Health = Current health of enemy
But I don't know how to keep it left, could I change the origin?
Please help, thanks!
Answer by almo · May 17, 2011 at 12:26 PM
I think you need to attach the healthbar to another (invisible) object so the place you want centered is at the center of the other object, and resize the other object.
Example:
Your healthbar is 2 units long. You make a blank object at 0, 0, 0. You then attach the healthbar to the blank. Then you set the healthbar's X coordinate to 1, 0, 0. Now the edge of the bar should be at the pivot point of the blank. Then scale the blank, and the edge of the bar should remain stationary.
Answer by Scribe · May 17, 2011 at 12:39 PM
Can you not use GUI's instead of having a plane?
GUI.BeginGroup (new Rect(10, 10, health, 30));
GUI.DrawTexture(new Rect (0, 0, Tex.width, Tex.height), Tex, ScaleMode.ScaleToFit);
GUI.EndGroup ();
you have this ^ in an OnGUI function and set the health to a number that represents the amount of health you have left (if you want it bigger or smaller you could do health*2 or health*0.75)
then you have your Tex could be a plain colour or a pattern or a gradient going green to red?
at the top add var Tex : Texture2D; and choose a texture