Graphics Reference
In-Depth Information
The Animation component should be attached to the gameObject that is to be ani-
mated. As this may not always be the same object as this script is attached to, the _anima-
tion variable used to coordinate animations later in the script is declared as public. Use the
Unity editor Inspector window to reference the Animation component:
public Animation _animation;
The enum keyword is used to declare an enumeration. An enumeration is a set of con-
stants called an enumeration list, which is a method of describing something with words
(keeping the code easily readable), yet at the same time, having the inner workings of the
engine use numbers to make processing more efficient. At the beginning of the script, we
start with the declaration of an enumeration called CharacterState. This will be used to
hold the state of the player's animation:
enum CharacterState {
Idle = 0,
Walking = 1,
Running = 2,
}
Most of the variable declarations are uneventful and do not require full descriptions,
although there are a few more variables of note:
CharacterState _characterState
This holds the player's animation state (based on the enumeration list
CharacterState shown earlier in this section).
BasePlayerManager myPlayerController
Although the player controller is not used by this script, it holds a
reference to it so that other scripts can find it easily.
Keyboard_Input default_input
The default input system is the standard keyboard input script.
CharacterController controller
Unity's built-in character controller is used for the physics simulation
of the character controlled by this script.
On to the Awake() function:
void Awake ()
{
The moveDirection variable is a Vector3 used to tell the character controller which
direction to move in. Here, it is given the world forward vector as a default value:
moveDirection = transform.TransformDirection(Vector3.forward);
The Animation component (a component built into Unity) is used to animate the
character. GetComponent() is used to find the instance of the animation component on
the gameObject this script is attached to:
// if _animation has not been set up in the inspector, we'll try to
// find it on the current gameobject
if(_animation==null)
_animation = GetComponent<Animation>();
Search WWH ::




Custom Search