Game Development Reference
In-Depth Information
2. The values of these new variables are used to modify the velocity:
3. Use these new [rt and [ru values to help you find the total distance required to move:
4. You can use this new distance value along with the OLAA@ constant to find the correct
5. Finally, you can rotate the object toward the target. This is the same formula you've been using
for rotation throughout the topic. The addition of + 90 is there to offset the rotation of the
robot object by 90 degrees. Without that, the leading edge of the robot would be its right side
because of the way the object was drawn in the symbol (with its “front” being the cone on the
robot's head). Any objects you use with this code might be oriented differently, so you'll prob-
ably want to adjust 5, to another number that you can figure out by trial and error when you
see the direction toward which your object rotates:
If the object is not within the robot's range, these directives kick in, which gradually slow it down by
using friction:
A bit of simple logic, a few careful adjustments to the easing formula, and you have a very effective
following behavior.
Running away from the player
It's very easy to create the exact opposite behavior: make the robot run away from the player. To see
this at work, bind the Nk^kp symbol to the Nk^kp[Nqj class. Test the project and you'll see the robot
flee from the player, as illustrated in Figure 10-19.
When I say that this is an opposite behavior, I mean that in the most literal sense imaginable. The
Nk^kp[Nqj class is exactly the same as Nk^kp[Bkhhks, except that three plus signs have been made
The rotation is negative so that the robot points in the opposite direction:
Search Nedrilad ::

Custom Search