Game Development Reference
In-Depth Information
Now we have to go through all targets stored in the array and test them one by one for
possible collisions with our bullet. We use foreach loop to go through the elements of the
array. The value of t changes at the beginning of each iteration of the loop, and takes the
value of the next element in the array until it goes through all elements. The first thing to
do in each iteration is to check whether the target has already been hit, and ignore it in if
this is true (line 17). After that, we find the distance between the bullet and the target using
Vector3.Distance() . If the distance is less than the “virtual” radius of the target, we count
this as a hit (lines 23 and 24), and hence set the hit flag in the target to true and destroy the
bullet (lines 27 and 30). I have mentioned that the radius of the target is virtual, since the
target is a cube and therefore doesn't actually have a radius. However, we try to estimate a
distance that can approximately simulate a border for the target object. Since the length of
each cube edge is one, we multiply it by 0.5 to get the minimal possible distance between
the surface of the cube and its center.
Once we have added these two scripts to the sphere that represent the bullet, we can create
the prefab of the bullet in a similar way to what we have done for the target. Since we do
not need a bullet in the scene at the beginning, you must delete the bullet object from the
scene after creating the prefab.
Search WWH ::




Custom Search