Creating Health Bars (VR)

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