Game Development Reference
In-Depth Information
The last function plays a reload animation found in
HandgunRig
and restores the
bulletsLeft
variable back to the value found in
bulletsPerClip
. As with other scripts
that play animations in the
HandgunRig
model, if you are not using that model, com-
ment out the animation lines.
function Reload () {
GameObject.Find(“HandgunRig”).animation.Play(“Reload”);
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (extraBullets > 0) {
extraBullets -= bulletsPerClip;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () {
return bulletsLeft;
}
11. Once the script is finished, place this script on the
HandGun
game object. Adjust the
variables so they appear as shown in Figure 10.16. To create a target reticule, add a
GUI Texture object to your scene and add a gun-crosshair image, like
aim.tif
found
in the online resources version of the project, to the
Texture
variable.
Figure 10.16
The variable values
that make this script
act like a handgun
The gun technically works but is an empty, so you cannot actually see it as it shoots
zombies. To remedy this, either import or create a new visible object (a cube or capsule
would work well), or utilize the
HandgunRig.fbx
model found in the online resources ver-
sion of the Unity project. Make whatever mesh you choose to be the visible portion of the
gun a child of the
HandGun
object. If you are using the
HandgunRig
model, the animation
lines of the script will play the gun's load, shoot, and reload animations.
1. Place whatever model you choose in a position appropriate for a first-person shooter,
typically on the lower-left portion of the screen.
2. Create a small capsule with the Hierarchy view's Create menu and place it roughly
where the tip of your gun mesh object is in your scene. Make it a child of the gun
object. Call this new capsule
muzzleFlash
.
Search WWH ::
Custom Search