Graphics Reference
In-Depth Information
The GetInput() function is virtual and is designed to be overridden if needed. This
makes it easier to switch out control systems if you derive a new class from this one for other
games. The variables horizontal_input and vertical_input are float values populated by the
input controller in this GetInput() function and used to control the player later in the script:
public virtual void GetInput ()
{
// this is just a 'default' function that (if needs be) should
// be overridden in the glue code
horizontal_input= default_input.GetHorizontal();
vertical_input= default_input.GetVertical();
}
The Update() function calls UpdateShip():
public virtual void Update ()
{
UpdateShip ();
}
public virtual void UpdateShip ()
{
// don't do anything until Init() has been run
if(!didInit)
return;
// check to see if we're supposed to be controlling the player
// before moving it
if(!canControl)
return;
GetInput();
When the amount to move is calculated (using the input values as a multiplier), we
use Time.deltaTime to make movement framerate independent. By multiplying the move-
ment amount by deltaTime (the time since the last update), the amount we move adapts
to the amount of time in between updates rather than only updating when the engine has
time to do so:
// calculate movement amounts for X and Z axis
moveXAmount = horizontal_input * Time.deltaTime * moveXSpeed;
moveZAmount = vertical_input * Time.deltaTime * moveZSpeed;
Quaternion rotations are the underlying form that angles take in the Unity engine.
You can use quaternions in your games, but they are notoriously difficult to get to grips
with. Thankfully, for the rest of us, there is an alternative in Euler angles. Euler angles are
a representation of a rotation in a three-dimensional vector, and it is much easier to define
or manipulate a rotation by x , y , and z values.
Unlike UnityScript, in C#, you cannot individually alter the values of each axis in
the rotation value of a transform. It has to be copied out into another variable and that
variable's values change before being copied back into the transform. In the code below,
the vector from eulerAngles is copied into a variable called tempRotation, and then its z
rotation value is altered to an arbitrary multiplier of the horizontal input. This is purely
Search WWH ::




Custom Search