Game Development Reference
In-Depth Information
Listing 2.9 Adding a Time Delay between Object Instantiation
. . .
private var state = "ATTACK";
private var rotationSpeed = 6.0;
private var attackDistance = 60;
private var retreatDistance = 10;
private var speed = 0.3;
private var reloadTime = 0.05;
private var lastShotTime = 0.0;
. . .
function Update ()
. . .
//act on state
if(state == "ATTACK")
if(lastShotTime + reloadTime < Time.fixedTime)
var newBullet:GameObject = Instantiate(aBullet,
lastShotTime = Time.fixedTime;
. . .
Step 14. Save and play. The Time.fixedTime value, stores the time since
the game started playing. The variable lastShotTime records when
the last bullet was shot, and reloadTime is the time allowed between
shots. If a time interval of reloadTime has passed since the last shot,
another bullet can be instantiated. Also, note that the rotationSpeed
for the rocket has increased to give it more attack time.
Step 15. To test if the bullets are hitting the target, open and modify
the code for the moveBullet script to that in Listing 2.10 . The code
tests the distance between a bullet and the base. If that distance is
less than 5, a hit is registered. For now, the hit is only displayed in a
print statement that shows up in the console.
Step 16. Save and play. When a bullet hits the base, the game will
print out hit in the message bar and console ( Figure 2.26 ). Double-
clicking on the message bar text will open the console.
Search Nedrilad ::

Custom Search