Animating the mole

Our moles will randomly popup from their holes so that the player can bash them with the mallet. To do this, we have to build a simple animator which tells the mole how and when to move:

  1. Create a new folder called Animators in the Project panel.
  2. Select Window | Animation from the main menu. We will use this window to set keyframes for the Mole GameObject's position.

The Animation window (shown in Figure 8. 4) is used to create animation clips for individual GameObjects. The following steps involve creating the popup animation for our mole. For the pop-down animation, we will play this animation in reverse:

Figure 8.4: Components of the timeline window
  1. With the Animation window open, select the MoleGame | MolePosition (1) | Mole GameObject. When a GameObject is selected, the Animation window will show a Create button.
  2. Click the Create button and set the Animation name to MolePopUp.anim in the Create New Animation dialog. Put the animation clip in the Animators folder.
  3. Click the Add Property button to reveal the properties to be animated.
  4. Drill down to Transform | Position and click the plus sign (+) for Position. This will add the Position property and entries for Position.x, Position.y, and Position.z values. Figure 8. 4 shows each of these values are set to 0.

One animation technique utilized by the Unity engine is a keyframe animation. This method is used to smoothly transition scene objects over time. To animate an object, we create at least two keyframes on a timeline: a starting keyframe and an ending keyframe. These keyframes set transformation properties that tell the game engine at this point in time put this object at (x1, y1, z1). Then at another point on the timeline, we set more keyframes that tell the engine now at this time, transform the object to (x2, y2, z2). In this fashion, we can animate position, rotation, and scale for GameObjects, colliders, and mesh renders. The method is called keyframing because we only set key frames and the software generates the in-between frames, creating smooth transitions:

  1. Scale the Animation window so that you can see both the starting and ending keyframes. In Unity 2017.3, the default keyframes are added at 0 and 1:00.
  2. Drag the primary key, located in the dark gray area above the timeline grid, from 1:00 to 0:30. This will move the ending keyframes (Position.x, Position.y, and Position.z) to the new position, changing the animation duration from one full-second to half-second.
  3. Click the timeline at 0:00 to reposition the scrub line. Changing keyframes requires the scrub line to be parked at the time where the change needs to occur.
  4. Click the 0 in the Position.y property field and change the value to -0.4.
  5. Hit the Play button in the Animation window to view the clip.

Our brief animation plays. Adjust the keyframe values if your mole doesn't recede completely below the GameTable's surface.

  1. Stop the animation by clicking Play button again.
  2. Select the MolePopUp clip in the Project window and uncheck the Loop Time checkbox. Because we will use a script to control the mole, we will not need to loop the animation.

Our animation is done. But Unity doesn't know how to handle the clip. If you hit the Scene Play button, you'll see the mole move, but it only pops up. It never recedes back into position; the animation clip cuts short and replays from the beginning. We are going to fix this with the state machine.

A state machine provides additional control for our animations. In the Zombie Shooting Gallery project, we used a state machine to control when each animation is triggered. Here we will use the function to control the pop-up and pop-down states.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.5