Game Development Reference
In-Depth Information
For our example, we are going to use a small red light with low radius and high intensity
to mark the current target. This marker can be referenced from the script via targetMarker .
At the beginning, we hide this marker by positioning it behind the player (remember that
the positions of the player and the camera are the same). During each frame update, we get
the ray that starts from the camera and passes through the mouse pointer. We then use this
ray in a ray cast test to check if there is an object under the mouse pointer. If such object
is found, we position the marker at the point where the ray hits the object. Notice that we
make the pointer look to our object and move it forward a little bit to make it visible. We
also make our object (the player) look at hit point. Since all weapons are children of the
player and have the same position and rotation of it, they are going to be targeted towards
the hit point as well. If ray casting did not detect any object under mouse pointer, we take
a far point (500 meters away) along the ray and look at it. In that case, the marker is posi-
tioned again behind the player to make it invisible.
After pointing the player (and consequently all weapons) correctly, we need to handle other
input commands: weapon switching, firing, and reloading. All these functions are handled
through WeaponController , which must be also added to player's game object. This script is
shown in Listing 84.
Search WWH ::




Custom Search