Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

Table of Contents (17 chapters)
Unity Game Development Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Housing our 3D UI


We will follow similar steps when housing our 3D UI as we did when housing our 2D UI. Create a new script and call it GUI_3D.

Creating a 3D health bar

Our first step will be to add our variables needed for the health bar:

public float currentHealth = 100;
public float maximumHealth = 100;
float currentBarLength;
public Transform HealthBar;
Vector3 OrigScale;

The first three variables are what we'll use to calculate our health bar. The Transform variable is how we'll interact with our 3D object that's being used as our health bar. The Vector3 variable is a reference point for when we scale the bar.

Our next step will be to add a Start() function. We'll use the Start() function to set the OrigScale variable:

void Start()
{
  OrigScale = HealthBar.transform.localScale;
}

We set OrigScale before we do anything else in the health bar. This is what we'll use as a reference point for the health bar. Next, we'll create our Update() function:

void Update()
{
  currentBarLength = currentHealth...