Game Development Reference
In-Depth Information
Vector3 cameraForward =
Camera.mainCamera.transform.TransformDirection
(Vector3.forward);
8. We project onto the horizontal plane because we want the character's motion
to be parallel to the horizontal plane rather than vary with the camera's angle.
We also use Normalize to ensure that the vector is well formed, as shown
in the following code:
cameraForward.y = 0.0f;
cameraForward.Normalize();
Also, note the trick whereby we find the right vector by flipping the x and z
components and negating the last component. This is faster than extracting
and transforming the right vector, but returns the same result shown in the
following code:
Vector3 cameraRight = new Vector3
(cameraForward.z, 0.0f,
-cameraForward.x);
9. We store the raw axis values from Unity's Input class. Recall that this is the
class that handles input for us, from which we can poll button and axes val-
ues. For h (which has a range from -1 to 1 ), the value between this range
corresponds to an amount of horizontal displacement on the analog stick,
joystick, or a keypress, as shown in the following code:
float v = Input.GetAxisRaw("Vertical");
For v (which ranges from -1 to 1 ), the value between this range corresponds
to an amount of vertical displacement of the analog stick, joystick, or a differ-
ent keypress.
float h = Input.GetAxisRaw("Horizontal");
To see the keybindings, please check the input class settings under Edit | Pro-
jectSettings | Input . There, under the Axes field in the object inspector, we can see
Search WWH ::




Custom Search