11. Add the follwing
12. And, finally, add the closing bracket for the animation block below the ProcessAudio line.
There’s only one little issue left to tackle before you can test the new code. In order for the audio clip to have the same delay as the animation clip, you left it inside the if(animates code block. Some objects, such as the LockPlate, don’t animate at all, and some objects don’t have an animation associated with all of their states. The solution is easy. You can copy the call to ProcessAudio and add it to an else statement.
1. Copy the ProcessAudio line.
2. Directly below the if(animates block's closing bracket, add:
3. Save the script.
Now you will be able to see your state machine in action. You should hear the audio, see the animation, and, if the object is selected, see its Current State change in the Inspector.
1. Select the Rock.
2. Click Play, pick the Rock, and watch its Current State change in the Interactor as it animates.
3. Select the KeyAtRock, pick it, and watch its Current State change as it animates.
4. Now select the LockPlate and pick it.
Unlike with the other two objects, its state doesn’t change. A quick look at its ObjectLookup data will show that a pick with the default cursor keeps it in state 1. To see it change, you will have to feed it a different cursor texture name.
5. In the LockPlate’s Interactor component, change the Cursor parameter to “Key Icon” (without the quotation marks) and pick it again.
This time, the Current State changes to 2. In the LookupState component, you will see that in order to put the LockPlate back to state 1, you will have to change the Lookup State 2 and set Cursor back to “default” (again, without the quotation marks).
6. Change Lookup State 2, Element 0 to: default,1
7. Change the Cursor back to “default” and pick it again.
The state goes back to 1. When you stop Play mode, the parameters will return to their previous values, but you can see how you can manually change values during runtime for testing purposes.
Now that you’ve got the primary animation handled, you can hook up the optional looping animation. Just as with the AniTwoState script, you will get the length of the animation before triggering the looping animation. By adding
the code after the first animation, its delay will already have finished. This part is pretty straightforward, especially as you have already added a Boolean variable to tell you if there is a looping animation, postLoop. The clip itself must be marked to loop (when importing or creating in Unity’s Animation editor), so all you have to do is trigger it.
1. Back in the Interactor script, in the ProcessObject function, below the ProcessAudio line, add the following: