Game Development Reference
In-Depth Information
that bullet travels 50 feet every frame. Unity then “sees” the bullet and checks
for collisions every 50 feet. So a thin object that was 10 feet, 20 feet, 30 feet,
40 feet from the gun would not be registered as hit by that bullet because
Unity simply wasn't looking when the bullet would have been in that object's
space. In fact, only objects that were 50 feet, 100 feet, 150 feet, 200 feet, and
so on from the gun would be registered as hit. Of course if the object were
50 feet thick, it would register, but we digress.
Frame miss is a very real problem for other issues as well. In one of the projects
we use here when we are briefing on our technology, we have a pipe filled with
particles flowing through it to show how we can illustrate building infrastructure.
The particles are colliding off the inside of the pipe that keeps the particle stream
contained. Occasionally, one of the particles gets moving so fast that Unity
misses the frame where it's supposed to calculate the end of the pipe and it
escapes, flying through the facility, and often then bouncing off walls and other
equipment. Although it's pretty funny for whoever is being briefed, it always
makes me cringe as it yanks the viewer right out of the experience of the space.
Avoiding frame miss is an important thing to be paying attention to in the
construction of a game.
Raycasting
One way to avoid issues involved with frame miss is to use a sort of sister
technology to collision detection called raycasting. Raycasting is the process
where an invisible ray is “cast” or thrown from an object. This ray is cast outward
(in the direction of the object's forward direction (Z)) and can be defined to
have a particular length (or to shoot out infinitely until it hits something).
Figure 14.1 shows a screenshot of a ray being cast from the camera attached
to the First Person Controller. The ray shoots out until it hits the wall right
ahead of it. The cool thing about this is that this ray is pretty smart and fast.
In one frame, the ray shoots out until it strikes a collider (thus avoiding frame
miss issues possible in things like collision detection) and the ray can report
back about what it's hit. The ray can tell us information about what the
object is, where in world space it was stopped by an object, and all sorts of
other useful data. We can utilize this data for some preemptive functions like
deciding whether a bullet would hit an object.
We are going to explore using raycasting for use in several situations. First, we
are going to make use of raycasting as an alternative to the OnMouseOver
functionality we used in the GUITextures tutorials. For GUITextures, OnMouseOver
works fairly reliably and is a great way to work. In the 3D world though, it's often
more accurate to highlight objects via raycasting than OnMouseOver.
Using raycasting we will highlight actionable items in the game. Namely, we'll
use this method to flip the main power switch in the base. Then later, we'll
also use it to identify and pick up a key that opens a locked door.
Search WWH ::




Custom Search