Game Development Reference
In-Depth Information
This script keeps shooting bullets as long as the player is pressing left control key. However,
there is a preset time gap between two consecutive bullets. The variable timeBetweenBullets
determines how many seconds must pass before a new bullet can be shot, and the variable
lastBulletTime stores the last time of the last shooting. In line 23, we subtract lastBulletTime
from the current time to get how many seconds passed so far since last shooting. If this
time is greater than timeBetweenBullets , then we instantiate a new bullet from the prefab.
The function Instantiate() takes a prefab to make a copy of, a position and a rotation for the
new object. We call this function in line 27 and pass to it the bullet prefab through bullet
variable. The position and rotation we provide to Instantiate() are the position and rotation
of the shuttle, which makes the bullet get out from the shuttle.
We use a similar technique in RocketLauncher script. We have, however, two differences.
The first difference is the use of discrete key presses on the space bar as a trigger for rockets,
unlike the continuous reading of left control key in BulletShooter . The second difference
is the limitation on the number of rockets that may exist in the scene simultaneously. In
lines 18 and 19 we get an array of all rockets in the scene by finding all objects of type
TargetFollower . Since this script exists only in rocket objects, the number of elements in the
array is exactly the same number of the rockets that are in the scene. If the length of this
array is zero, then there are no rockets in the scene and we may instantiate a new one (lines
22 through 27). Illustration 33 shows a screen shot of the demo. The result can also be seen
in scene9 in the accompanying project.
Search WWH ::




Custom Search