Game Development Reference
In-Depth Information
HealthBar.transform.LookAt(Camera.main.transform);
if(Input.GetButton("Fire1"))
{
currentHealth -= 1.00f;
ChangeBar();
}
}
We will use
currentBarLength
to scale our object, so we set it by dividing
cur-
rentHealth
by
maximumHealth
. This will give us a value less than or equal to 1
and will scale our health bar perfectly. Next, we tell our
HealthBar
quad to look at
our camera; this will allow us to always see it in 3D space. For testing purposes, we
subtract the
currentHealth
value and call the
ChangeBar()
function when we
press the left mouse button.
Our final step in creating the 3D health bar is to create the
ChangeBar()
function:
void ChangeBar()
{
HealthBar.transform.localScale =
Vector3.Lerp(OrigScale, new
Vector3(currentBarLength,
OrigScale.y,OrigScale.z), Time.time);
}
Here, we set the
localScale
value of the
HealthBar
quad by lerping from
OrigScale
to our new scale. The new scale uses
CurrentBarLength
to determ-
ine the width of our health bar. When you press the left mouse button while testing
the scene, you'll see the health bar go down over time.
Creating 3D damage reports
The damage reports will show up every time damage is done to our enemy. It'll pop
up above the enemy for a brief amount of time and then disappear again. To start
things off, we'll add a few variables to our script for the damage reports: