introduction_2d.rst 14 KB

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