introduction_2d.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. Click on the AnimationPlayer node in the Node tab to open the Animation
  28. 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 five 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 keyframe editor (when enabled)
  40. - The timeline and track controls, where you can zoom the timeline and
  41. edit tracks for example.
  42. See the `animation panel reference <#animation-panel-reference>`__ below
  43. for details.
  44. Computer animation relies on keyframes
  45. --------------------------------------
  46. A keyframe defines the value of a property at a certain point in time.
  47. White and blue diamond shapes represent keyframes in the timeline.
  48. .. figure:: img/animation_keyframes.png
  49. :alt: Keyframes in Godot
  50. Keyframes in Godot
  51. The engine interpolates values between keyframes, resulting in a gradual
  52. change in values over time.
  53. .. figure:: img/animation_illustration.png
  54. :alt: Two keyframes are all it takes to obtain a smooth motion
  55. Two keyframes are all it takes to obtain a smooth motion
  56. The timeline lets you insert keyframes and change their timing. It also
  57. defines how long the animation is.
  58. .. figure:: img/animation_timeline.png
  59. :alt: The timeline in the animation panel
  60. The timeline in the animation panel
  61. Each line of the Animation Panel is an animation track. Normal and
  62. Transform tracks reference node properties. Their name or id is a path
  63. to the node and the affected property.
  64. .. figure:: img/animation_normal_track.png
  65. :alt: Example of Normal animation tracks
  66. Example of Normal animation tracks
  67. .. tip::
  68. If you animate the wrong property, you can edit a track's path anytime.
  69. Double click on it and type the new path. Play the animation using the
  70. "Play from beginning" button |Play from beginning| (Default shortcut:
  71. Shift+D) to see the changes instantly.
  72. Tutorial: Creating a simple animation
  73. -------------------------------------
  74. Scene setup
  75. ~~~~~~~~~~~
  76. For this tutorial, we'll going to create an AnimationPlayer node and a
  77. sprite node as an AnimationPlayer node child.
  78. .. figure:: img/animation_animation_player_tree.png
  79. :alt: Our scene setup
  80. Our scene setup
  81. The sprite holds an image texture and we animate that sprite to move
  82. between two points on the screen. As a starting point, move the sprite
  83. to a left position on the screen.
  84. .. tip::
  85. Adding animated nodes as children to the AnimationPlayer node is not
  86. required, but it is a nice way of distinguishing animated nodes from
  87. non-animated nodes in the Scene Tree.
  88. Select the AnimationPlayer node and click on "Add animation" (|Add
  89. Animation|) in the animation tab to add a new animation.
  90. Enter a name for the animation in the 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 on
  107. "Create".
  108. This creates a new track and our first keyframe at the beginning of
  109. the timeline:
  110. .. figure:: img/animation_track.png
  111. :alt: The sprite track
  112. The sprite track
  113. The track name consists of a Node Path, followed by a colon, followed by
  114. a reference to its property, that we would like to modify.
  115. In our example, the path is ``AnimationPlayer/Sprite`` and the property
  116. is ``position``.
  117. The path always starts at the AnimationPlayer node's parent (so paths
  118. always have to include the AnimationPlayer node itself).
  119. .. note::
  120. Don't worry if you change the names of nodes in the Scene Tree, that you
  121. already have tracks for. Godot automatically updates the paths in the
  122. tracks.
  123. The second keyframe
  124. ~~~~~~~~~~~~~~~~~~~
  125. Now we need to set the destination where our sprite should be headed and
  126. how much time it takes to get there.
  127. Let's say, we want it to take 2 seconds to go to the other point. By
  128. default the animation is set to last only 1 second, so change this in
  129. the timeline controls in animation panel's lower panel to 2.
  130. .. figure:: img/animation_set_length.png
  131. :alt: Animation length
  132. Animation length
  133. Click on the timeline header near the 2 second mark and move the sprite
  134. to the target destination on the right side.
  135. Again, click the key button in the toolbar. This creates our second
  136. keyframe.
  137. Run the animation
  138. ~~~~~~~~~~~~~~~~~
  139. Click on the "Play from beginning" (|Play from beginning|) button.
  140. Yay! Our animation runs:
  141. .. figure:: img/animation_simple.gif
  142. :alt: The animation
  143. The animation
  144. Back and forth
  145. ~~~~~~~~~~~~~~
  146. As you can see, the "loop" button is enabled by default and our
  147. animation loops. Godot has an additional feature here. Like said before,
  148. Godot always calculates the frames between two keyframes. In a loop, the
  149. first keyframe is also the last keyframe, if no keyframe is specified at
  150. the end.
  151. .. figure:: img/animation_loop.png
  152. :alt: Animation loop
  153. Animation loop
  154. If you set the animation length to 4 seconds now, the animation moves
  155. back and forth. You can change this behaviour if you change the track's
  156. loop mode. This is covered in the next chapter.
  157. Track settings
  158. ~~~~~~~~~~~~~~
  159. Each track has a settings panel at the end, where you can set the update
  160. rate, the track interpolation, and the loop mode.
  161. .. figure:: img/animation_track_settings.png
  162. :alt: Track settings
  163. Track settings
  164. The update rate of a track tells Godot when to update the property
  165. values. This can be:
  166. - Continuous: Update the property on each frame
  167. - Discrete: Only update the property on keyframes
  168. - Trigger: Only update the property on keyframes or triggers
  169. .. figure:: img/animation_track_rate.png
  170. :alt: Track rate
  171. Track rate
  172. In normal animations, you usually use "Continuous". The other types are
  173. used to script complex animations.
  174. The interpolation tells Godot how to calculate the frame values between
  175. the keyframes. These interpolation modes are supported:
  176. - Nearest: Set the nearest keyframe value
  177. - Linear: Set the value based on a linear function calculation between
  178. the two keyframes
  179. - Cubic: Set the value based on a cubic function calculation between
  180. the two keyframes
  181. .. figure:: img/animation_track_interpolation.png
  182. :alt: Track interpolation
  183. Track interpolation
  184. Cubic interpolation leads to a more natural movement, where the
  185. animation is slower at a keyframe and faster between keyframes. This is
  186. usually used for character animation. Linear interpolation creates more
  187. of a robotic movement.
  188. Godot supports two loop modes, which affect the animation if it's set to
  189. loop:
  190. .. figure:: img/animation_track_loop_modes.png
  191. :alt: Loop modes
  192. Loop modes
  193. - Clamp loop interpolation: When this is selected, the animation stops
  194. after the last keyframe for this track. When the first keyframe is
  195. reached again, the animation will reset to its values.
  196. - Wrap loop interpolation: When this is selected, Godot calculates the
  197. animation after the last keyframe to reach the values of the first
  198. keyframe again.
  199. Keyframes for other properties
  200. ------------------------------
  201. Godot doesn't restrict you to only edit transform properties. Every
  202. property can be used as a track where you can set keyframes.
  203. If you select your sprite while the animation panel is visible, you get
  204. a small keyframe button for all of the sprite's properties. Click on
  205. this button and Godot automatically adds a track and keyframe to the
  206. current animation.
  207. .. figure:: img/animation_properties_keyframe.png
  208. :alt: Keyframes for other properties
  209. Keyframes for other properties
  210. Edit keyframes
  211. --------------
  212. For advanced use and to edit keyframe in detail, enable the keyframe
  213. editor (|Keyframe editor|).
  214. This adds an editor pane on the right side of the track settings. When
  215. you select a keyframe, you can directly edit its values in this editor:
  216. .. figure:: img/animation_keyframe_editor_key.png
  217. :alt: Keyframe editor editing a key
  218. Keyframe editor editing a key
  219. Additionally, you can also edit the transition value for this keyframe:
  220. .. figure:: img/animation_keyframe_editor_transition.png
  221. :alt: Keyframe editor editing a transition
  222. Keyframe editor editing a transition
  223. This tells Godot, how to change the property values when it reaches this
  224. keyframe.
  225. You usually tweak your animations this way, when the movement doesn't
  226. "look right".
  227. Advanced: Call Func tracks
  228. --------------------------
  229. Godot's animation engine doesn't stop here. If you're already
  230. comfortable with Godot's scripting language
  231. :ref:`doc_gdscript` and :doc:`/classes/index` you
  232. know that each node type is a class and has a bunch of callable
  233. functions.
  234. For example, the :ref:`class_AudioStreamPlayer` node type has a
  235. function to play an audio stream.
  236. Wouldn't it be great to play a stream at a specific keyframe in an
  237. animation? This is where "Call Func Tracks" come in handy. These tracks
  238. reference a node again, this time without a reference to a property.
  239. Instead, a keyframe holds the name and arguments of a function, that
  240. Godot should call when it reaches this keyframe.
  241. To let Godot play a sample when it reaches a keyframe, follow this list:
  242. Add a :ref:`class_AudioStreamPlayer` to the Scene Tree and setup a
  243. stream using an audio file you put in your project.
  244. .. figure:: img/animation_add_audiostreamplayer.png
  245. :alt: Add AudioStreamPlayer
  246. Add AudioStreamPlayer
  247. Click on "Add track" (|Add track|) on the animation panel's track
  248. controls.
  249. Select "Add Call Func Track" from the list of possible track types.
  250. .. figure:: img/animation_add_call_func_track.png
  251. :alt: Add Call Func Track
  252. Add Call Func Track
  253. Select the :ref:`class_AudioStreamPlayer` node in the selection
  254. window. Godot adds the track with the reference to the node.
  255. .. figure:: img/animation_select_audiostreamplayer.png
  256. :alt: Select AudioStreamPlayer
  257. Select AudioStreamPlayer
  258. Select the timeline position, where Godot should play the sample by
  259. clicking on the timeline header.
  260. Enable the Keyframe Editor by clicking on |Keyframe editor|.
  261. Click on "Add keyframe" near the settings of our func track (|Add
  262. keyframe|) and select the keyframe.
  263. .. figure:: img/animation_call_func_add_keyframe.png
  264. :alt: Add a keyframe to the call func track
  265. Add a keyframe to the call func track
  266. Enter "play" as the function name.
  267. .. figure:: img/animation_call_func_keyframe.png
  268. :alt: Keyframe settings of a call func track
  269. Keyframe settings of a call func track
  270. When Godot reaches the keyframe, Godot calls the
  271. :ref:`class_AnimationPlayer` node's "play" function and the stream
  272. plays.
  273. References
  274. ----------
  275. Animation panel reference
  276. ~~~~~~~~~~~~~~~~~~~~~~~~~
  277. .. figure:: img/animation_animation_panel_reference.png
  278. :alt: The animation panel reference
  279. The animation panel reference
  280. The animation panel has the following parts (from left to right):
  281. Animation controls
  282. ^^^^^^^^^^^^^^^^^^
  283. .. figure:: img/animation_reference_animation_controls.png
  284. :alt: Animation controls
  285. Animation controls
  286. - Play animation backwards from current position
  287. - Play animation backwards from the animation end
  288. - Stop animation
  289. - Play animation forwards from the animation beginning
  290. - Play animation forwards from the current position
  291. - Direct time selection
  292. Animation management
  293. ^^^^^^^^^^^^^^^^^^^^
  294. .. figure:: img/animation_reference_animation_management.png
  295. :alt: Animation management
  296. Animation management
  297. - Create a new animation
  298. - Load animation
  299. - Save animation
  300. - Duplicate animation
  301. - Rename animation
  302. - Delete animation
  303. - Animation selection
  304. - Automatically play selected animation
  305. - Edit animation blend times
  306. - Extended animation Tools
  307. Timeline zoom level control
  308. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  309. .. figure:: img/animation_reference_timeline_zoom_level.png
  310. :alt: Timeline zoom level control
  311. Timeline zoom level control
  312. Timeline control
  313. ^^^^^^^^^^^^^^^^
  314. .. figure:: img/animation_reference_timeline_controls.png
  315. :alt: Timeline control
  316. Timeline control
  317. - Length of animation
  318. - Steps of animation
  319. - Toggle loop animation
  320. Track control
  321. ^^^^^^^^^^^^^
  322. .. figure:: img/animation_reference_track_control.png
  323. :alt: Track control
  324. Track control
  325. - Add track
  326. - Move track up
  327. - Move track down
  328. - Delete track
  329. - Extended track tools
  330. - Toggle keyframe editor
  331. .. |Play from beginning| image:: img/animation_play_from_beginning.png
  332. .. |Add Animation| image:: img/animation_add.png
  333. .. |Keyframe editor| image:: img/animation_keyframe_editor_toggle.png
  334. .. |Add track| image:: img/animation_add_track.png
  335. .. |Add keyframe| image:: img/animation_add_keyframe.png