Game Development Reference
In-Depth Information
How it works...
Each time the player presses the ToggleCover key or button, a check will be run to see
whether there is cover within range. Three rays are cast forward from a low height, one at
the left edge of the model, one from the center, and one from the right. Since leftDir is
multiplied by -1, 0, and 1 on the x and z axes, we get the offset to the left- and right-hand
side of the center position. To be considered inside cover, all three must have collided with
something. This ensures that the player model is wholly covered.
The Ray won't stop just because it collides with something, and if the cover is thin, it might
continue through the back side of it, generating additional collisions. We only want to
count one collision per ray, though (the closest), which is why we only increase lowCol-
lisions by one.
The high cover is checked after the low cover, because in general, there is never any cover
that only covers the upper body.
Once it's decided that the character is inside cover and the player wants to move, we need
to check whether the player will still be inside cover at the new position. This is so that the
player doesn't accidentally exit cover and end up getting killed. To avoid unnecessary per-
formance hits, we don't want to do this every frame. We do this only if there has actually
been some movement happening.
Search WWH ::




Custom Search