Creating a 3-frame animation clip to make a platform continually animate

In this recipe, we'll make a wooden-looking platform continually animate, moving upwards and downwards. This can be achieved with a single, 3-frame, animation clip (starting at top, position at bottom, top position again).

Creating a 3-frame animation clip to make a platform continually animate

Getting ready

This recipe builds on the previous one, so make a copy of that project, and work on the copy for this recipe.

How to do it...

To create a continually moving animated platform, follow these steps:

  1. Drag an instance of the sprite platformWoodBlocks from the Project | Sprites folder into the scene. Position this GameObject at (-4, -5, 0), so that these wood blocks are neatly to left, and slightly below, the wall blocks platform.
  2. Add a Box Collider 2D component to GameObject platformWoodBlocks so that the player's character can stand on this platform too. Choose menu: Add Component | Physics 2D | Box Collider 2D.
  3. Create a new folder named Animations, in which to store the animation clip and controller we'll create next.
  4. Ensuring GameObject platformWoodBlocks is still selected in the Hierarchy, open an Animation panel, and ensure it is in Dope Sheet view (this is the default).
  5. Click the empty dropdown menu in the Animation panel (next to the greyed out word 'Samples'), and choose menu item [Create New Clip].
  6. Save the new clip in your Animations folder, naming it 'platform-wood-moving-up-down'.
  7. Click button Add Curve, and chose Transform and the click the '+' plus-sign by Position. We are now recording changes to the (X, Y, Z) position of GameObject platformWoodBlocks in this animation clip.
  8. You should now see 2 'keyframes' at 0.0 and at 1.0. These are indicated by diamonds in the Timeline area in the right-hand-section of the Animation panel.
  9. We need 3 keyframes, with the new one at 2:00 seconds. Click at 2:00 in the Timeline along the top of the Animation panel, so that the red line for the current playhead time is at time 2:00. Then click diamond+ button to create a new keyframe at the current playhead time:
    How to do it...
  10. The first and third keyframes are fine – they record the current height of the wood platform at Y= -5. We need to make the middle keyframe record the height of the platform at the top of its motion, and Unity in-betweening will do all the rest of the animation work for us. Select the middle keyframe (at time 1:00), by clicking on either diamond at time 1:00 (they should both turn blue, and the red playhead vertical line should move to 1:00, to indicate the middle keyframe is being edited).
  11. Now in the Inspector change the Y position of the platform to 0. You should see that all three X, Y, Z values have a red background in the Inspector – this is to inform you that the values of the Transform component are being recorded in the animation clip.
  12. Save and run your scene. The wooden platform should now be animating continuously, moving smoothly up and down the positions we setup.

How it works...

You have added an animation to GameObject platformWoodBlocks. This animation contains three keyframes. A keyframe represents the values of properties of the object at a point in time. The first keyframe stores a Y-value of -4, the second keyframe a Y-value of 0, and the final keyframe -4 again. Unity calculates all the in-between values for us, and the result is a smooth animation of the Y-position of the platform.

Note

Note: If we wanted to duplicate the moving platform, first we'd need to create a new, empty GameObject named movingBlockParent, and then parent platformWoodBlocks to this GameObject. Duplicating GameObject movingBlockParent would then allow us to create more moving blocks in our scene. If we simply duplicated platformWoodBlocks directly, then when the scene runs each duplicate would be animated back to the location of the original animation frames (that is, all copies would be positioned and moving in the original location).

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

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