Graphics Reference
In-Depth Information
Having input separated into a single GetInput() function should make it easy to cus-
tomize inputs if required. Here, the horz, vert, and jumpButton Boolean variables are set
by the default_input (a Keyboard_Input.cs instance) functions. These variables are used
to control the character controller further in the script:
public virtual void GetInput()
{
horz= Mathf.Clamp( default_input.GetHorizontal() , -1, 1 );
vert= Mathf.Clamp( default_input.GetVertical() , -1, 1 );
}
LateUpdate() is used to call for an update to the input Boolean variables via the
GetInput() script. Unity advises LateUpdate as the best place to check for keyboard entry,
after the engine has carried out its own input updates:
public virtual void LateUpdate()
{
// we check for input in LateUpdate because Unity recommends
// this
if(canControl)
GetInput();
}
This script was adapted from the third-person controller code provided by Unity. Its
original behavior was to move the player based on the direction of the camera. For exam-
ple, when the right key was pressed, the player would move toward the right of the screen
based on the camera location and rotation (right being along the camera's x -axis). Player
rotation in the original script was determined by the camera rotation, and for the move-
ment to make sense, it was reliant on the camera being behind the player during gameplay.
Of course, this control scheme would not work with a top-down camera system or any
camera system other than the one it was intended for.
As the script was adapted, the dependence on the camera rotation was removed and
replaced with a directional approach. In this system, pressing the right key moves right
along the world x -axis and pressing up or down moves up or down along the world z -axis.
For some top-down character-based games, the directional approach is a good one,
but the genre is not exactly locked into this scheme so we also provide a rotational move-
ment approach. Whenever moveDirectionally is false, pressing the left or right button will
rotate the player left or right around its y -axis. Pressing the up button will walk forward
along the player's z -axis, and pressing down will move it backward.
In the UpdateSmoothedMovementDirection() script, the moveDirectionally vari-
able is checked to determine which type of movement function to call to update player
movement:
void UpdateSmoothedMovementDirection ()
{
if(moveDirectionally)
{
UpdateDirectionalMovement();
} else {
UpdateRotationMovement();
}
}
Search WWH ::




Custom Search