Game Development Reference
In-Depth Information
like this, it can be tough to know what items the player can actually do
something to and which are there for visual fodder. Different games handle
this in different ways, but a fairly consistent convention is to have the item
glow, or change color, or otherwise highlight when the player moves his
mouse or crosshairs over the item. This is the convention we are going to
build.
Raycasting will do this really well. We'll set up a raycasting mechanism that
shoots a ray from the camera and shoots it straight forward. When the center
of the screen (where we will place a crosshairs) lines up on an object, the ray
will strike it and let us know what the name of that object is. In the script we
will compare the names of the objects the ray strikes with a list of actionable
items; if it matches, we will turn up all the material's color values so that the
item looks white hot. When the ray is not striking the object, the material will
return to its regular color.
To do this, we'll need to make use of a crosshairs image. There is one included
with the 2D Assets package imported in earlier tutorials (it's called Crosshairs)
or you can make your own. The image should be a power of 2 in size (mine is
32×32) and have an alpha channel built into it in Photoshop.
We'll be doing these steps within the Scene-Hallway scene within Unity
(continuing on from the last chapter).
Step 1: Create crosshairs. Create a GUITexture to house the crosshairs
(GameObject>Create Other>GUI Texture). Rename the GameObject
Crosshairs . Use the Crosshairs image within the 2D Assets folder as the
Texture for the GUITexture. If using the provided image, change the Pixel
Inset X and Y to -16 and the Width and Height to 32 .
Step 2: Reduce the transparency of the crosshairs. With the Crosshairs
GUITexture object selected, click the color swatch and reduce the Alpha
(the A slider) to around 50 ( Figure 14.5 ).
Figure 14.5 Setting up the
crosshairs.
Search WWH ::




Custom Search