Creating Health Bars (VR)

I didn’t understand the enemy health bar chapter because it’s not shown how to build a health bar step for step and only showed some code and drap drop already finished health bar.
Could you explain me how I set the UI up in Unity and what is important to set the health bar above any 3d object?

Hi!

Creating a health bar is outside the scope of that chapter. If you want to know how to use Unity’s UI features, check out our tutorial series on just that:

https://www.raywenderlich.com/149464/introduction-to-unity-ui-part-1

To create a world space UI, you have to change the Render Mode on the Canvas to World Space.
Is there anything specific you had trouble with while adding the health bars?

Cheers!

Not anything specific yet, because I’m a beginner.
I’d just really like to know from the start how to create a health bar in VR like the one in the book.

Edit: Ah one specific problem I had, in the chapter it wasn’t explained how to spawn the health bar above an enemy object.

I can’t explain every detail unfortunately, but I can try to help you on your way. If there are terms you don’t know yet, be sure to google them.

That health bar started out as a regular screen space one. It’s basically a Slider with all graphics removed.
If you drag the prefab into the scene you can take a look at the pieces that create that look. They keep looking at the player’s headset as well so you can always see them, this is called “billboarding”.

The spawning of health bars is explained on page 545 of v1.3 of the book. The UIManager creates them using this piece of code:

public void CreateHealthBarForEnemy(Enemy enemy) {
 //2
 GameObject healthBar = Instantiate(enemyHealthBarPrefab);
 //3
 healthBar.transform.SetParent(enemyHealthBars, false);
 //4
 healthBar.GetComponent<EnemyHealthBar>().enemy = enemy;
}

The way they position themselves above the enemy is done in the EnemyHealthBar.cs script. The billboarding code is also in there, so you should definitely take a look at that to know how it all works.

The VR chapter is one of the more advanced ones in the book and it assumes that the reader is already familiar with most of the basics of using Unity, so I totally understand that it can be a bit confusing for beginners. I’d recommend starting with the other chapters and checking out some of the tutorials on the website

Stuff like this is easier to grasp if you slowly build up your knowledge instead of jumping off the the deep end.

I hope this helps!

Cheers

Thank you!
When I drag the healthbar prefab into game scene, I can see the components in the hierachy, but the health bar is invisible in the scene, what could be the reason? (it’s activated!)

@blackdragonbe Can you please help with this when you get a chance? Thank you - much appreciated! :]

Can you see the health bar if you select it in the inspector and press F on your keyboard?

This topic was automatically closed after 166 days. New replies are no longer allowed.