Game Development Reference
In-Depth Information
Notice that the function of Collector script is very abstract: it checks only whether there
are collisions with collectables, and calls Collect() function from the colliding collectables.
Collision check is performed by comparing the distance between the collector and the
collectable with the sum of their virtual radii (line 27). Radii are virtual because the
collector and the collectable are not necessarily spherical shapes, but this method is enough
to serve the purpose in our case. If a collision is detected, the collector calls Collect()
function of the collectable, and gives itself as a value for owner parameter. As you see, a
script can get the value of itself by using the word this (line 30).
Now we have the mechanism that can detect a collision between a collector and a
collectable. Next step is to determine what should be done after this collision. Theoretically,
there is an infinite number of collectables, and each one of these need to be handled differ-
ently. For example, coins increase the amount of money the player has, while health portions
restore player's health. In our example game, we have two main types of collectables: coins
and food. Coins are going to increase the amount of money in the category box, while food
increase the size of the ball (player character) with a specific factor for limited time. By
increasing the size of the ball, food helps the player to collect coins faster. There are two
types of food: green and red, and each one of them has its own factor if size increment as
well as time limit. Before going into the details of these collectables, let's have a quick look
at Listing 28, which shows YRotator , a simple script that rotates an object around the global
y axis with specific speed.
Search WWH ::




Custom Search