introduction_2d.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. Introduction to the 2D animation features
  2. =========================================
  3. Overview
  4. --------
  5. The :ref:`class_AnimationPlayer` node allows you to create anything
  6. from simple to complex animations.
  7. In this guide you learn to:
  8. - Work with the Animation Panel
  9. - Animate any property of any node
  10. - Create a simple animation
  11. - Call functions with the powerful Call Function Tracks
  12. In Godot you can animate anything you find in the Inspector. Animations
  13. are changes to properties over time. This means you can animate anything
  14. visual in nature: sprites, UI elements, particles, the visibility, and
  15. color of textures. But not only. You can also control gameplay related
  16. values 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 track modifiers and 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. On the computer animations rely on keyframes. A keyframe defines the
  47. 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.
  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 a 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, wether it should set it up for us. Click on
  108. "Create".
  109. This creates a new track and our very 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 ``transform/pos``.
  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 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.
  157. Track settings
  158. ~~~~~~~~~~~~~~
  159. Each track has a settings panel at the end, where you can set the update
  160. rate and the track interpolation.
  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 curved 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. Keyframes for other properties
  189. ------------------------------
  190. Godot doesn't restrict to only edit transform properties. Every property
  191. 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 of 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 keyframe in detail, enable the keyframe
  202. editor (|Keyframe editor|).
  203. This adds an editor pane on the right side of the track settings. When
  204. you select a keyframe, you can directly edit its values in this editor:
  205. .. figure:: img/animation_keyframe_editor_key.png
  206. :alt: Keyframe editor editing a key
  207. Keyframe editor editing a key
  208. Additionally, you can also edit the transition value for this keyframe:
  209. .. figure:: img/animation_keyframe_editor_transition.png
  210. :alt: Keyframe editor editing a transition
  211. Keyframe editor editing a transition
  212. This tells Godot, how to change the property values when it reaches this
  213. keyframe.
  214. You usually tweak your animations this way, when the movement doesn't
  215. "look right".
  216. Advanced: Call Func tracks
  217. --------------------------
  218. Godot's animation engine doesn't stop here. If you're already
  219. comfortable with Godot's scripting language GDScript and API you know
  220. that each node type is a class and has a bunch of callable functions.
  221. For example, the :ref:`class_SamplePlayer2D` node type has a function
  222. to play a sample.
  223. Wouldn't it be great to play a sample at a specific keyframe in an
  224. animation? This is where "Call Func Tracks" come in handy. These tracks
  225. reference a node again, this time without a reference to a property.
  226. Instead a keyframe holds the name and arguments of a function, that
  227. Godot should call when reaching this keyframe.
  228. To let Godot play a sample when it reaches a keyframe, follow this list:
  229. Add a SamplePlayer2D to the Scene Tree and add a sample library and a
  230. sample to it.
  231. .. figure:: img/animation_add_sampleplayer2d.png
  232. :alt: Add SamplePlayer2D
  233. Add SamplePlayer2D
  234. Click on "Add track" (|Add track|) on the animation panel's track
  235. controls.
  236. Select "Add Call Func Track" from the list of possible track types.
  237. .. figure:: img/animation_add_call_func_track.png
  238. :alt: Add Call Func Track
  239. Add Call Func Track
  240. Select the SamplePlayer2D node in the selection window. Godot adds the
  241. track with the reference to the node.
  242. .. figure:: img/animation_select_sampleplayer2d.png
  243. :alt: Select SamplePlayer2D
  244. Select SamplePlayer2D
  245. Select the timeline position, where Godot should play the sample by
  246. clicking on the timeline header.
  247. Enable the Keyframe Editor by clicking on |Keyframe editor|.
  248. Click on "Add keyframe" near the settings of our func track (|Add
  249. keyframe|) and select the keyframe.
  250. .. figure:: img/animation_call_func_add_keyframe.png
  251. :alt: Add a keyframe to the call func track
  252. Add a keyframe to the call func track
  253. Enter "play" as the function name and set the argument counter to 1.
  254. Select "String" as the first argument type and use the sample name as
  255. the argument value.
  256. .. figure:: img/animation_call_func_keyframe.png
  257. :alt: Keyframe settings of a call func track
  258. Keyframe settings of a call func track
  259. When Godot reaches the keyframe, Godot calls the SamplePlayer2D node's
  260. "play" function with the sample name.
  261. References
  262. ----------
  263. Animation panel reference
  264. ~~~~~~~~~~~~~~~~~~~~~~~~~
  265. .. figure:: img/animation_animation_panel_reference.png
  266. :alt: The animation panel reference
  267. The animation panel reference
  268. The animation panel has the following parts (from left to right):
  269. Animation controls
  270. ^^^^^^^^^^^^^^^^^^
  271. .. figure:: img/animation_reference_animation_controls.png
  272. :alt: Animation controls
  273. Animation controls
  274. - Play animation backwards from current position
  275. - Play animation backwards from the animation end
  276. - Stop animation
  277. - Play animation forwards from the animation beginning
  278. - Play animation forwards from the current position
  279. - Direct time selection
  280. Animation management
  281. ^^^^^^^^^^^^^^^^^^^^
  282. .. figure:: img/animation_reference_animation_management.png
  283. :alt: Animation management
  284. Animation management
  285. - Create a new animation
  286. - Load animation
  287. - Save animation
  288. - Duplicate animation
  289. - Rename animation
  290. - Delete animation
  291. - Animation selection
  292. - Automatically play selected animation
  293. - Edit animation blend times
  294. - Extended animation Tools
  295. Timeline zoom level control
  296. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  297. .. figure:: img/animation_reference_timeline_zoom_level.png
  298. :alt: Timeline zoom level contro
  299. Timeline zoom level contro
  300. Timeline control
  301. ^^^^^^^^^^^^^^^^
  302. .. figure:: img/animation_reference_timeline_controls.png
  303. :alt: Timeline control
  304. Timeline control
  305. - Length of animation
  306. - Steps of animation
  307. - Toggle loop animation
  308. Track control
  309. ^^^^^^^^^^^^^
  310. .. figure:: img/animation_reference_track_control.png
  311. :alt: Track control
  312. Track control
  313. - Add track
  314. - Move track up
  315. - Move track down
  316. - Delete track
  317. - Extended track tools
  318. - Toggle keyframe editor
  319. .. |Play from beginning| image:: img/animation_play_from_beginning.png
  320. .. |Add Animation| image:: img/animation_add.png
  321. .. |Keyframe editor| image:: img/animation_keyframe_editor_toggle.png
  322. .. |Add track| image:: img/animation_add_track.png
  323. .. |Add keyframe| image:: img/animation_add_keyframe.png