Game Development Reference
In-Depth Information
Step 11. Attach the code to the large cube. Select the cube in
the Hierarchy; in the Inspector, set the duckObj variable of the
releaseDuck script to the duck prefab. Now when the car drives up
onto the platform, a new duck will be released. Note that the collision
in the script is checked that the car is the one doing the colliding, as
otherwise each time a duck is created it would cause the collision to
create another duck, ad infinitum. The name of the collision object
is “chassis collider,” as this is the name of the child object of the car
prefab that has the collider attached.
Step 12. As with the previous game, create another large box
collider for a finish line. Ensure that the Is Trigger box is selected.
Step 13. Assign the tag duck for the duck prefab. All created ducks
will have this tag.
Step 14. Create a JavaScript file called finish.js . This script will be
attached to the finish line collider and count the number of ducks
going through it. Use the code in Listing 4.22 to register the duck
count.
Listing 4.22 Counting Ducks That Pass Through a Collider
var duckCount = 0;
function OnTriggerEnter(collision: Collider)
{
if(collision.gameObject.tag == "duck")
{
duckCount++;
Destroy(collision.gameObject);
}
}
Step 15. Attach finish.js to the finish line. Note that after a duck is
counted it is destroyed. This will ensure that the player can't keep
pushing the duck back and forth over the finish line to get extra
points.
Step 16. Next, download Chapter Four/corfont.ttf from the Web site.
This is the font used to display the time and score. Add the font to the
Project. Select it and give it a size of 32 in the Inspector, as shown in
Figure 4.14 . Press the Apply button. Note that once you have changed
the size of this font, it will stay this size for all the places you use it. If
you want to use the same font with a different size in the same game,
rename the font file, drag it into the Project for another copy, and
change the size of the new one.
 
Search Nedrilad ::




Custom Search