Game Development Reference
In-Depth Information
8.
Create the two functions that change the clips:
public void OpenHat () {
animator.SetBool("Armed", true);
}
public void CloseHat () {
animator.SetBool("Armed", false);
}
The first line checks for the existence of the Animator component. The next two lines wait for an
on key up event from the virtual keys you created earlier. If a key press is detected and finished ,
the function is called that tells the Animator to toggle the parameter on that triggers the Gnome
arming animation. The arming state goes directly into the Gnome armed state when it has finished.
If you wanted to trigger a weapon firing, you would use GetButtonDown or GetKeyDown where the
message is sent at the start of the action rather than the end. The animator parameter changes are
sent from their own functions so that they can be triggered by events other than key presses, which
is also why the little functions are marked as public .
9.
Save the script.
10.
Drag it onto the GardenGnome object (where the Animator component resides).
11.
Make sure the Weight of the Laser layer is set to 1 .
12.
Click Play, and press the 1 key to trigger the arming sequence.
13.
Press the 2 key to disarm and close the hat.
14.
Update the Gnomatic Garden Defender prefab.
As a fail-safe for the Weight value resetting to 0 after an adjustment, you can add it to the script to
avoid having to remember to set it to 1 every time you make adjustments to the transitions.
A search of the Unity community will tell you that the Weight assignment is also lost if the Animator
component is deactivated and reactivated. The community also suggests that using OnEnable rather
than Start is the best place to put the code. OnEnable is evaluated before Start , so you will have to
identify the animator there as well. The bug may have been fixed by now, but it certainly won't hurt
to use OnEnable just in case.
Create the OnEnable function as follows:
1.
void OnEnable () {
if (animator.layerCount >=2) { // if there is more than one layer...
// set the Laser layer's weight to 1, (base layer is index 0, so Laser is 1)
animator.SetLayerWeight(1, 1f); // layer index, weight
}
}
Move the animator= line from the Start function to the top of the OnEnable
function.
2.
 
Search WWH ::




Custom Search