Refining the Pick
Now that you’ve got the metadata actions working for the primary object, it’s time to revisit the cursor behavior. When you pick the objects, there are a couple of things that ought to happen. First, you should prevent the player from picking anything while the animation plays out. While you’re at it, it would also be nice to hide the cursor completely while the action is in play.
Let’s begin by making use of the processing variable you added earlier in the chapter. If there are no matches to the pick, nothing will happen, so it will make more sense to block picks and turn off the cursor as soon as the ProcessObject function is called. Let’s start with the picks.
1. Open the Interactor script.
2. Add the following line at the top of the ProcessObject function:
With the flag set, you will have to bypass both the mouse pick and the mouseover.
3. Add the following line to the OnMouseDown and OnMouseEnter functions, below the distance checks and at the top of the OnMouseExit function:
Next, you will have to decide when to turn the flag back off. If there is an animation, it should be turned off after that. You’ve already got a pause the length of the animation in the if postLoop section, but if it doesn’t have a loop animation, you’d have to add one as an else afterwards. If you are going to do that, you may as well move the one you’ve already got up above the if postLoop section.
4. Move the yield new WaitForSeconds(currentAnimationClip.length) line and its comment up out of the if(postLoop) line, just above its comment.
5. Below the if(postLoop) closing bracket, add the following:
If there was no animation, you will have to wait a token amount of time after playing the audio before turning off the flag. A second or so should do the job.
6. Inside the final else clause, under the ProcessAudio (currentSound), add the following:
7. Save the script.
8. Click Play and test by clicking the flower several times, watching the console report.
When the cursor once again changes color, you will know that your pick will go through. The flower doesn’t animate again, because it was set to state 2, unpickable, but the pick had to be processed to know that.
If you test the Rock, then the KeyAtRock, you will find that you can pick the KeyAtRock right away. The reason for the picking discrepancy is that the processing flag is not global; each instance of it belongs only to the object its script is on. This lets you block re-picks on the same object, but not picks on other objects. While this may at first sound undesirable, preventing the player from picking the newly revealed object immediately can prove frustrating.
You also may have noticed a real problem with the Rock. A drawback of the Store In Root (New) animation import type is that starting a new clip from the same Animation component stops the currently playing animation wherever it happens to be on the time line. Until the Legacy Animation is fully integrated into Mecanim, the easiest workaround is to duplicate the AnimationObjects group, deactivate or delete the Rock from the first group, and the rest of the objects from the second.