Graphics Reference
In-Depth Information
Just like many of the other scripts in this topic, ater the variable declarations, the
Start() function uses a Boolean called didInit to track whether or not the script has been
initialized.
public virtual void Start()
{
// we are overriding Start() so as not to call Init, as we want
// the game controller to do this in this game.
didInit=false;
this.Init();
}
Rather than having all of the initialization in the Start() function, it has its own func-
tion called Init() to keep things tidy. Init() begins by caching references to the transform,
gameObject, and rigidbody.
public virtual void Init ()
{
// cache refs to our transform and gameObject
myTransform= transform;
myGO= gameObject;
myBody= rigidbody;
Input comes from an instance of Keyboard_Controller.cs held in a variable called
default_input, which we will look into in detail later in this chapter.
// add default keyboard input
default_input= myGO.AddComponent<Keyboard_Input>();
To keep the player within a certain space in the play area, this script uses the z posi-
tion it starts at to work out how to limit movement. A float named originZ is set:
// grab the starting Z position to use as a baseline for Z
// position limiting
originZ=myTransform.localPosition.z;
Once the Init() function has finished, didInit is set to true.
// set a flag so that our Update function knows when we are OK to use
didInit=true;
}
For the script to function correctly within the framework, all player scripts use a
Boolean variable named canControl to decide whether or not the object should be con-
trolled by a third party such as AI or input. Note that this is not the same as enabling or
disabling player movement—it just decides whether or not to allow control of the player.
The function called GameStart() is called by the game controller script when the time is
right to allow control to begin.
public virtual void GameStart ()
{
// we are good to go, so let's get moving!
canControl=true;
}
Search WWH ::




Custom Search