Game Development Reference
In-Depth Information
You can also create parameters for use in scripts to trigger transitions based on user input or any
other condition appropriate to your game. Click the Animator window tab, and in the bottom left
corner create a float parameter by clicking on the Parameters + symbol and selecting Float. Change
the name from New Float to Speed. Select the transition arrow in the Inspector, change Exit Time to
Speed, and select Greater and 0.1.
You'll also want to transition from Walk back to Idle. In the same fashion, right-click Walk, create a
new transition, and connect it to the Idle state. Select the transition, and for the Conditions property
select Speed, this time Less, and 0.1.
If you playtest now, the zombie stays in the Idle state. You will need to create a script that defines what
user input meets the conditions that trigger the transition from one state to another. In the Project panel,
create a new JavaScript script and name it ZombieLocomotion. Drag and drop it onto the zombie_
lowres game object in the Hierarchy view to attach it to the zombie character. The ZombieLocomotion
script will appear as a new component in the Inspector. Double-click the ZombieLocomotion script icon
in the Project panel to open it in MonoDevelop, then edit the code as follows:
#pragma strict
private var animator : Animator;
function Start () {
animator = GetComponent(Animator);
}
function Update () {
animator.SetFloat("Speed", Input.GetAxis("Vertical"));
}
This code breaks down as follows:
(1) private var animator : Animator;
Declare an Animator type reference variable. If you did this in one line in the Start() function like
you did previously in the OnAnimatorMove() function in the ZombieMotion script, the scope of the
animator variable would be limited to the Start() function. By declaring it here, you make it available
to any function within the ZombieLocomotion class.
(2) animator.SetFloat("Speed", Input.GetAxis("Vertical"));
Here you are setting the Speed parameter you created based on the user input. Playtest and you'll
find the zombie walks forward when the up arrow key or the W key is pressed.
 
Search WWH ::




Custom Search