introduction_2d.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. .. _doc_introduction_2d_animation:
  2. Introduction to the 2D animation features
  3. =========================================
  4. Overview
  5. --------
  6. The :ref:`class_AnimationPlayer` node allows you to create anything
  7. from simple to complex animations.
  8. In this guide you learn to:
  9. - Work with the Animation Panel
  10. - Animate any property of any node
  11. - Create a simple animation
  12. - Call functions with the powerful Call Function Tracks
  13. In Godot, you can animate anything available in the Inspector, such as
  14. Node transforms, sprites, UI elements, particles, visibility and color
  15. of materials, and so on. You can also modify values of script variables
  16. and call any function.
  17. Create an AnimationPlayer node
  18. ------------------------------
  19. To use the animation tools we first have to create an
  20. :ref:`class_AnimationPlayer` node.
  21. The AnimationPlayer node type is the data container for your animations.
  22. One AnimationPlayer node can hold multiple animations, that can
  23. automatically transition to one another.
  24. .. figure:: img/animation_create_animationplayer.png
  25. :alt: The AnimationPlayer node
  26. The AnimationPlayer node
  27. After creating one click on the AnimationPlayer node in the Node tab to
  28. open the Animation Panel at the bottom of the viewport.
  29. .. figure:: img/animation_animation_panel.png
  30. :alt: The animation panel position
  31. The animation panel position
  32. It consists of four parts:
  33. .. figure:: img/animation_animation_panel_overview.png
  34. :alt: The animation panel
  35. The animation panel
  36. - Animation controls (i.e. add, load, save, and delete animations)
  37. - The tracks listing
  38. - The timeline with keyframes
  39. - The timeline and track controls, where you can zoom the timeline and
  40. edit tracks for example.
  41. Computer animation relies on keyframes
  42. --------------------------------------
  43. A keyframe defines the value of a property at a certain point in time.
  44. Diamond shapes represent keyframes in the timeline. A line between two
  45. keyframes indicates that the value hasn't changed.
  46. .. figure:: img/animation_keyframes.png
  47. :alt: Keyframes in Godot
  48. Keyframes in Godot
  49. The engine interpolates values between keyframes, resulting in a gradual
  50. change in values over time.
  51. .. figure:: img/animation_illustration.png
  52. :alt: Two keyframes are all it takes to obtain a smooth motion
  53. Two keyframes are all it takes to obtain a smooth motion
  54. The timeline lets you insert keyframes and change their timing. It also
  55. defines how long the animation is.
  56. .. figure:: img/animation_timeline.png
  57. :alt: The timeline in the animation panel
  58. The timeline in the animation panel
  59. Each line of the Animation Panel is an animation track. Normal and
  60. Transform tracks reference node properties. Their name or id is a path
  61. to the node and the affected property.
  62. .. figure:: img/animation_normal_track.png
  63. :alt: Example of Normal animation tracks
  64. Example of Normal animation tracks
  65. .. tip::
  66. If you animate the wrong property, you can edit a track's path anytime.
  67. Double click on it and type the new path. Play the animation using the
  68. "Play from beginning" button |Play from beginning| (or pressing
  69. :kbd:`Shift + D` on keyboard) to see the changes instantly.
  70. Tutorial: Creating a simple animation
  71. -------------------------------------
  72. Scene setup
  73. ~~~~~~~~~~~
  74. For this tutorial, we'll create an AnimationPlayer node and a sprite node as its
  75. child.
  76. .. figure:: img/animation_animation_player_tree.png
  77. :alt: Our scene setup
  78. Our scene setup
  79. The sprite holds an image texture. We animate that sprite to move
  80. between two points on the screen. For this tutorial, use the default Godot
  81. icon as the sprite's texture. As a starting point, move the sprite
  82. to a left position on the screen.
  83. .. tip::
  84. Adding animated nodes as children to the AnimationPlayer node is not
  85. required, but it is a nice way of distinguishing animated nodes from
  86. non-animated nodes in the Scene Tree.
  87. Select the AnimationPlayer node, then click the "Animation" button in the
  88. animation editor. From the list select "New" (|Add
  89. Animation|) to add a new animation. And Enter a name for the animation in the
  90. dialog box.
  91. .. figure:: img/animation_create_new_animation.png
  92. :alt: Add a new animation
  93. Add a new animation
  94. Adding a track
  95. ~~~~~~~~~~~~~~
  96. To add a new track for our sprite, select it and take a look in the
  97. toolbar:
  98. .. figure:: img/animation_convenience_buttons.png
  99. :alt: Convenience buttons
  100. Convenience buttons
  101. These switches and buttons allow you to add keyframes for the selected
  102. node's location, rotation, and scale respectively.
  103. Deselect rotation, because we are only interested in the location of our
  104. sprite for this tutorial and click on the key button.
  105. As we don't have a track already set up for the transform/location
  106. property, Godot asks whether it should set it up for us. Click **Create**.
  107. This creates a new track and our first keyframe at the beginning of
  108. the timeline:
  109. .. figure:: img/animation_track.png
  110. :alt: The sprite track
  111. The sprite track
  112. The second keyframe
  113. ~~~~~~~~~~~~~~~~~~~
  114. Now we need to set the destination where our sprite should be headed and
  115. how much time it takes to get there.
  116. Let's say, we want it to take 2 seconds to go to the other point. By
  117. default the animation is set to last only 1 second, so change this in
  118. the timeline controls in animation panel's lower panel to 2.
  119. .. figure:: img/animation_set_length.png
  120. :alt: Animation length
  121. Animation length
  122. Click on the timeline header near the 2-second mark and move the sprite
  123. to the target destination on the right side.
  124. Again, click the key button in the toolbar. This creates our second
  125. keyframe.
  126. Run the animation
  127. ~~~~~~~~~~~~~~~~~
  128. Click on the "Play from beginning" (|Play from beginning|) button.
  129. Yay! Our animation runs:
  130. .. figure:: img/animation_simple.gif
  131. :alt: The animation
  132. The animation
  133. Back and forth
  134. ~~~~~~~~~~~~~~
  135. Godot has an additional feature here. Like said before,
  136. Godot always calculates the frames between two keyframes. In a loop, the
  137. first keyframe is also the last keyframe, if no keyframe is specified at
  138. the end.
  139. .. figure:: img/animation_loop.png
  140. :alt: Animation loop
  141. Animation loop
  142. If you set the animation length to 4 seconds now, the animation moves
  143. back and forth. You can change this behavior if you change the track's
  144. loop mode. This is covered in the next chapter.
  145. Track settings
  146. ~~~~~~~~~~~~~~
  147. Each track has a settings panel at the end, where you can set the update
  148. mode, the track interpolation, and the loop mode.
  149. .. figure:: img/animation_track_settings.png
  150. :alt: Track settings
  151. Track settings
  152. The update mode of a track tells Godot when to update the property
  153. values. This can be:
  154. - Continuous: Update the property on each frame
  155. - Discrete: Only update the property on keyframes
  156. - Trigger: Only update the property on keyframes or triggers
  157. - Capture: The current value of a property is remembered, and it will blend with the first animation key found
  158. .. figure:: img/animation_track_rate.png
  159. :alt: Track mode
  160. Track mode
  161. In normal animations, you usually use "Continuous". The other types are
  162. used to script complex animations.
  163. The interpolation tells Godot how to calculate the frame values between
  164. the keyframes. These interpolation modes are supported:
  165. - Nearest: Set the nearest keyframe value
  166. - Linear: Set the value based on a linear function calculation between
  167. the two keyframes
  168. - Cubic: Set the value based on a cubic function calculation between
  169. the two keyframes
  170. .. figure:: img/animation_track_interpolation.png
  171. :alt: Track interpolation
  172. Track interpolation
  173. Cubic interpolation leads to a more natural movement, where the
  174. animation is slower at a keyframe and faster between keyframes. This is
  175. usually used for character animation. Linear interpolation creates more
  176. of a robotic movement.
  177. Godot supports two loop modes, which affect the animation if it's set to
  178. loop:
  179. .. figure:: img/animation_track_loop_modes.png
  180. :alt: Loop modes
  181. Loop modes
  182. - Clamp loop interpolation: When this is selected, the animation stops
  183. after the last keyframe for this track. When the first keyframe is
  184. reached again, the animation will reset to its values.
  185. - Wrap loop interpolation: When this is selected, Godot calculates the
  186. animation after the last keyframe to reach the values of the first
  187. keyframe again.
  188. Keyframes for other properties
  189. ------------------------------
  190. Godot doesn't restrict you to only edit transform properties. Every
  191. property can be used as a track where you can set keyframes.
  192. If you select your sprite while the animation panel is visible, you get
  193. a small keyframe button for all the sprite's properties. Click on
  194. this button and Godot automatically adds a track and keyframe to the
  195. current animation.
  196. .. figure:: img/animation_properties_keyframe.png
  197. :alt: Keyframes for other properties
  198. Keyframes for other properties
  199. Edit keyframes
  200. --------------
  201. For advanced use and to edit keyframes in detail, You can click on them
  202. to bring up the keyframe editor in the inspector. You can use this to
  203. directly edit its values.
  204. .. figure:: img/animation_keyframe_editor_key.png
  205. :alt: Keyframe editor editing a key
  206. Keyframe editor editing a key
  207. Additionally, you can also edit the easing value for this keyframe by
  208. clicking and dragging the easing setting. This tells Godot, how to change
  209. the property values when it reaches this keyframe.
  210. You usually tweak your animations this way, when the movement doesn't
  211. "look right".
  212. Advanced: Call Method tracks
  213. ----------------------------
  214. Godot's animation engine doesn't stop here. If you're already
  215. comfortable with Godot's scripting language
  216. :ref:`doc_gdscript` and :doc:`/classes/index` you
  217. know that each node type is a class and has a bunch of callable
  218. methods.
  219. For example, the :ref:`class_AudioStreamPlayer` node type has a
  220. method to play an audio stream.
  221. Wouldn't it be great to use a method at a specific keyframe in an
  222. animation? This is where "Call Method Tracks" come in handy. These tracks
  223. reference a node again, this time without a reference to a property.
  224. Instead, a keyframe holds the name and arguments of a method, that
  225. Godot should call when it reaches this keyframe.
  226. To demonstrate, we're going to use a call method track to play audio at a
  227. specific keyframe. Normally to play audio you should use an audio track,
  228. but for the sake of demonstrating methods we're going to do it this way.
  229. Add a :ref:`class_AudioStreamPlayer` to the Scene Tree and setup a
  230. stream using an audio file you put in your project.
  231. Click on "Add track" (|Add track|) on the animation panel's track
  232. controls.
  233. Select "Add Call Method Track" from the list of possible track types.
  234. .. figure:: img/animation_add_call_method_track.png
  235. :alt: Add Call Method Track
  236. Add Call Method Track
  237. Select the :ref:`class_AudioStreamPlayer` node in the selection
  238. window. Godot adds the track with the reference to the node.
  239. .. figure:: img/animation_select_audiostreamplayer.png
  240. :alt: Select AudioStreamPlayer
  241. Select AudioStreamPlayer
  242. Right click the timeline where Godot should play the sample and
  243. click the "Insert Key" option. This will bring up a list of methods
  244. that can be called for the AudioStreamPlayer node. Select the first
  245. one.
  246. .. image:: img/animation_method_options.png
  247. When Godot reaches the keyframe, Godot calls the
  248. :ref:`class_AudioStreamPlayer` node's "play" function and the stream
  249. plays.
  250. You can change its position by dragging it on the timeline, you can also
  251. click on the keyframe and use the keyframe settings in the inspector.
  252. .. image:: img/animation_call_method_keyframe.png
  253. .. |Play from beginning| image:: img/animation_play_from_beginning.png
  254. .. |Add Animation| image:: img/animation_add.png
  255. .. |Add track| image:: img/animation_add_track.png