Game Development Reference
In-Depth Information
//AI HP
// Create one Group to contain both images
// Adjust the first 2 coordinates to place it somewhere else on-
screen
GUI.BeginGroup (Rect (110,47,156,21));
// Draw the background image
GUI.DrawTexture(Rect (0,0,156,21), frameTexture);
// Create a second Group which will be clipped
// We want to clip the image and not scale it, which is why we
need the second Group
GUI.BeginGroup (Rect (0,0,ai.GetHpPercent() * 156, 21));
// Draw the foreground image
GUI.DrawTexture (Rect (0,0,156,21), aiTexture);
// End both Groups
GUI.EndGroup ();
GUI.EndGroup ();
}
In this funcion, we just use the new
GUI
funcion,
GUI.BeginGroup()
, to draw
the mask for the hit-point bar.
5. Now, we will go to back to Unity and drag the
HitPointUI.js
script to the
HitPointUI
game object in the
Hierarchy
view. Then we will go to its
Inspector
view and set the following:
Hit Point UI (Script)
Ai
:
AI
(Drag the
AI
game object inside the
Enemy
game object to the
Hierarchy
view here)
Player
:
First Person Controller
(Drag the
First Person Controller
game object to the
Hierarchy
view here)
Frame Texture
:
hitPointFrame
(Drag the
hitPointFrame.png
from the
Chapter6/UI/
folder here)
Hp Texture
:
hitPointBarHP
(Drag the
hitPointBarHP.png
from the
Chapter6/UI/
folder here)
Ai Texture
:
hitPointEnemy
(Drag the
hitPointEnemy.png
from the
Chapter6/UI/
folder here)
Text Hp Texture
:
HP
(drag the
HP.png
from the
Chapter6/UI/
folder
here)
Text Ai Texture
:
Enemy
(drag the
ENEMY.png
from the
Chapter6/UI/
folder here)
Now we have finished our game, so click on
Play
to see the result. We will see that when the
player or enemy gets shot the hit-point bar will decrease.