Game Development Reference
In-Depth Information
The Angle() function calculates the angle between two rotations. It might
be used to determine if an enemy is facing directly toward or away from
the player.
Slerp() takes a starting rotation and an ending rotation and cuts it up
into small arcs. It is extremely effective in making rotating objects
change from one direction to another in a smooth motion. Originally the
previously created rocket ship was flipping back and forth as it changed
direction. This occurs when the objects goes from the original-facing
direction to the goal-facing direction in one frame. Slerp() breaks this
rotation up so that small parts of it occur with each frame instead of all in
one go.
Now that you have a little understanding of quaternions, we can continue
examining the Move() function from Listing 2.5 . It contains two quaternion
functions: LookRotation() and Slerp(). LookRotation() takes a vector and turns
it into a quaternion. It is being used here to convert the direction vector into
a quaternion. This is required by the Slerp() function, which uses the rotation
speed and the time between the drawing of frames (Time.deltaTime) to carve
up the complete turn angle into smaller pieces, making the rocket ship turn
around in a smooth motion.
Unity Hands On
Step 1. To create bullets that come from the rocket, create a plane by
selecting GameObject > Create Other > Plane from the main menu. If
a small white plane does not appear in the Game or Scene, select the
new Plane from the Hierarchy and set its position values to (0,0,0). It
should appear somewhere near the planet.
Step 2. Create a red-colored material and add it to the plane.
Step 3. Set the scale x , y , and z values for the plane to (0.05,0.05,
0.05). It will now appear as a small red dot on the screen.
Step 4. Create a new JavaScript file and call it moveBullet . Enter the
code shown in Listing 2.6 and attach it to the plane.
Listing 2.6 Script to Move an Object in Its Forward Direction
at a Certain Speed
var speed = 1.0;
function Update ()
this.transform.Translate(Vector3.forward * speed);
Search Nedrilad ::

Custom Search