introduction.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. .. _doc_introduction_animation:
  2. Introduction to the animation features
  3. ======================================
  4. The :ref:`class_AnimationPlayer` node allows you to create anything
  5. from simple to complex animations.
  6. In this guide you learn to:
  7. - Work with the Animation Panel
  8. - Animate any property of any node
  9. - Create a simple animation
  10. In Godot, you can animate anything available in the Inspector, such as
  11. Node transforms, sprites, UI elements, particles, visibility and color
  12. of materials, and so on. You can also modify values of script variables
  13. and even call functions.
  14. Create an AnimationPlayer node
  15. ------------------------------
  16. To use the animation tools we first have to create an
  17. :ref:`class_AnimationPlayer` node.
  18. The AnimationPlayer node type is the data container for your animations.
  19. One AnimationPlayer node can hold multiple animations, which can
  20. automatically transition to one another.
  21. .. figure:: img/animation_create_animationplayer.webp
  22. :alt: The AnimationPlayer node
  23. The AnimationPlayer node
  24. After you create an AnimationPlayer node, click on it to
  25. open the Animation Panel at the bottom of the viewport.
  26. .. figure:: img/animation_animation_panel.webp
  27. :alt: The animation panel position
  28. The animation panel position
  29. The animation panel consists of four parts:
  30. .. figure:: img/animation_animation_panel_overview.webp
  31. :alt: The animation panel
  32. The animation panel
  33. - Animation controls (i.e. add, load, save, and delete animations)
  34. - The tracks listing
  35. - The timeline with keyframes
  36. - The timeline and track controls, where you can zoom the timeline and
  37. edit tracks, for example.
  38. Computer animation relies on keyframes
  39. --------------------------------------
  40. A keyframe defines the value of a property at a point in time.
  41. Diamond shapes represent keyframes in the timeline. A line between two
  42. keyframes indicates that the value doesn't change between them.
  43. .. figure:: img/animation_keyframes.png
  44. :alt: Keyframes in Godot
  45. Keyframes in Godot
  46. You set values of a node's properties and create animation keyframes for them.
  47. When the animation runs, the engine will interpolate the values between the
  48. keyframes, resulting in them gradually changing over time.
  49. .. figure:: img/animation_illustration.png
  50. :alt: Two keyframes are all it takes to obtain a smooth motion
  51. Two keyframes are all it takes to obtain a smooth motion
  52. The timeline defines how long the animation will take. You can insert keyframes
  53. at various points, and change their timing.
  54. .. figure:: img/animation_timeline.png
  55. :alt: The timeline in the animation panel
  56. The timeline in the animation panel
  57. Each line in the Animation Panel is an animation track that references a
  58. Normal or Transform property of a node. Each track stores a path to
  59. a node and its affected property. For example, the position track
  60. in the illustration refers to the ``position`` property of the Sprite2D
  61. node.
  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 at any time
  67. by double-clicking on it and typing 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 a Sprite node with an AnimationPlayer as
  75. its child. We will animate the sprite to move between two points on the screen.
  76. .. figure:: img/animation_animation_player_tree.png
  77. :alt: Our scene setup
  78. Our scene setup
  79. .. warning::
  80. AnimationPlayer inherits from Node instead of Node2D or Node3D, which means
  81. that the child nodes will not inherit the transform from the parent nodes
  82. due to a bare Node being present in the hierarchy.
  83. Therefore, it is not recommended to add nodes that have a 2D/3D transform
  84. as a child of an AnimationPlayer node.
  85. The sprite holds an image texture. For this tutorial, select the Sprite2D node,
  86. click Texture in the Inspector, and then click Load. Select the default Godot
  87. icon for the sprite's texture.
  88. Adding an animation
  89. ~~~~~~~~~~~~~~~~~~~
  90. Select the AnimationPlayer node and click the "Animation" button in the
  91. animation editor. From the list, select "New" (|Add Animation|) to add a new
  92. animation. Enter a name for the animation in the dialog box.
  93. .. figure:: img/animation_create_new_animation.webp
  94. :alt: Add a new animation
  95. Add a new animation
  96. Manage an animation libraries
  97. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. For reusability, the animation is registered in a list in the animation library resource. If you add an animation to AnimationPlayer without specifying any particular settings, the animation will be registered in the [Global] animation library that AnimationPlayer has by default.
  99. .. figure:: img/animation_library.webp
  100. :alt: Manage animations
  101. Manage animations
  102. If there are multiple animation libraries and you try to add an animation, a dialog box will appear with options.
  103. .. figure:: img/animation_library_dialog.webp
  104. :alt: Add a new animation with library option
  105. Add a new animation with library option
  106. Adding a track
  107. ~~~~~~~~~~~~~~
  108. To add a new track for our sprite, select it and take a look at the
  109. toolbar:
  110. .. figure:: img/animation_convenience_buttons.png
  111. :alt: Convenience buttons
  112. Convenience buttons
  113. These switches and buttons allow you to add keyframes for the selected
  114. node's location, rotation, and scale. Since we are only animating the sprite's
  115. position, make sure that only the location switch is selected. The selected
  116. switches are blue.
  117. Click on the key button to create the first keyframe. Since we don't have a
  118. track set up for the Position property yet, Godot will offer to
  119. create it for us. Click **Create**.
  120. Godot will create a new track and insert our first keyframe at the beginning of
  121. the timeline:
  122. .. figure:: img/animation_track.webp
  123. :alt: The sprite track
  124. The sprite track
  125. The second keyframe
  126. ~~~~~~~~~~~~~~~~~~~
  127. We need to set our sprite's end location and how long it will take for it to get there.
  128. Let's say we want it to take two seconds to move between the points. By
  129. default, the animation is set to last only one second, so change the animation
  130. length to 2 in the controls on the right side of the animation panel's timeline
  131. header.
  132. .. figure:: img/animation_set_length.png
  133. :alt: Animation length
  134. Animation length
  135. Now, move the sprite right, to its final position. You can use the *Move tool* in the
  136. toolbar or set the *Position*'s X value in the *Inspector*.
  137. Click on the timeline header near the two-second mark in the animation panel
  138. and then click the key button in the toolbar to create the second keyframe.
  139. Run the animation
  140. ~~~~~~~~~~~~~~~~~
  141. Click on the "Play from beginning" (|Play from beginning|) button.
  142. Yay! Our animation runs:
  143. .. figure:: img/animation_simple.gif
  144. :alt: The animation
  145. The animation
  146. Back and forth
  147. ~~~~~~~~~~~~~~
  148. Godot has an interesting feature that we can use in animations. When Animation
  149. Looping is set but there's no keyframe specified at the end of the animation,
  150. the first keyframe is also the last.
  151. This means we can extend the animation length to four seconds now, and Godot
  152. will also calculate the frames from the last keyframe to the first, moving
  153. our sprite back and forth.
  154. .. figure:: img/animation_loop.png
  155. :alt: Animation loop
  156. Animation loop
  157. You can change this behavior by changing the track's loop mode. This is covered
  158. in the next chapter.
  159. Track settings
  160. ~~~~~~~~~~~~~~
  161. Each track has a settings panel at the end, where you can set its update
  162. mode, track interpolation, and loop mode.
  163. .. figure:: img/animation_track_settings.webp
  164. :alt: Track settings
  165. Track settings
  166. The update mode of a track tells Godot when to update the property
  167. values. This can be:
  168. - **Continuous:** Update the property on each frame
  169. - **Discrete:** Only update the property on keyframes
  170. - **Capture:** if the first keyframe's time is greater than ``0.0``, the
  171. current value of the property will be remembered and
  172. will be blended with the first animation key. For example, you
  173. could use the Capture mode to move a node that's located anywhere
  174. to a specific location.
  175. .. figure:: img/animation_track_rate.webp
  176. :alt: Track mode
  177. Track mode
  178. You will usually use "Continuous" mode. The other types are used to
  179. script complex animations.
  180. Track interpolation tells Godot how to calculate the frame values between
  181. keyframes. These interpolation modes are supported:
  182. - Nearest: Set the nearest keyframe value
  183. - Linear: Set the value based on a linear function calculation between
  184. the two keyframes
  185. - Cubic: Set the value based on a cubic function calculation between
  186. the two keyframes
  187. - Linear Angle (Only appears in rotation property): Linear mode with shortest path rotation
  188. - Cubic Angle (Only appears in rotation property): Cubic mode with shortest path rotation
  189. .. figure:: img/animation_track_interpolation.webp
  190. :alt: Track interpolation
  191. Track interpolation
  192. With Cubic interpolation, animation is slower at keyframes and faster between
  193. them, which leads to more natural movement. Cubic interpolation is commonly
  194. used for character animation. Linear interpolation animates changes at a fixed
  195. pace, resulting in a more robotic effect.
  196. Godot supports two loop modes, which affect the animation when it's set to
  197. loop:
  198. .. figure:: img/animation_track_loop_modes.webp
  199. :alt: Loop modes
  200. Loop modes
  201. - Clamp loop interpolation: When this is selected, the animation stops
  202. after the last keyframe for this track. When the first keyframe is
  203. reached again, the animation will reset to its values.
  204. - Wrap loop interpolation: When this is selected, Godot calculates the
  205. animation after the last keyframe to reach the values of the first
  206. keyframe again.
  207. Keyframes for other properties
  208. ------------------------------
  209. Godot's animation system isn't restricted to position, rotation, and scale.
  210. You can animate any property.
  211. If you select your sprite while the animation panel is visible, Godot will
  212. display a small keyframe button in the *Inspector* for each of the sprite's
  213. properties. Click on one of these buttons to add a track and keyframe to
  214. the current animation.
  215. .. figure:: img/animation_properties_keyframe.webp
  216. :alt: Keyframes for other properties
  217. Keyframes for other properties
  218. Edit keyframes
  219. --------------
  220. You can click on a keyframe in the animation timeline to display and
  221. edit its value in the *Inspector*.
  222. .. figure:: img/animation_keyframe_editor_key.webp
  223. :alt: Keyframe editor editing a key
  224. Keyframe editor editing a key
  225. You can also edit the easing value for a keyframe here by clicking and dragging
  226. its easing curve. This tells Godot how to interpolate the animated property when it
  227. reaches this keyframe.
  228. You can tweak your animations this way until the movement "looks right."
  229. .. |Play from beginning| image:: img/animation_play_from_beginning.png
  230. .. |Add Animation| image:: img/animation_add.png
  231. Using RESET tracks
  232. ------------------
  233. You can set up a special *RESET* animation to contain the "default pose".
  234. This is used to ensure that the default pose is restored when you save
  235. the scene and open it again in the editor.
  236. For existing tracks, you can add an animation called "RESET" (case-sensitive),
  237. then add tracks for each property that you want to reset.
  238. The only keyframe should be at time 0, and give it the desired default value
  239. for each track.
  240. If AnimationPlayer's **Reset On Save** property is set to ``true``,
  241. the scene will be saved with the effects of the reset animation applied
  242. (as if it had been seeked to time ``0.0``).
  243. This only affects the saved file – the property tracks in the editor stay
  244. where they were.
  245. If you want to reset the tracks in the editor, select the AnimationPlayer node,
  246. open the **Animation** bottom panel then choose **Apply Reset** in the
  247. animation editor's **Edit** dropdown menu.
  248. When adding tracks on new animations, the editor will ask you to automatically
  249. create a RESET track when using the keyframe icon next to a property in the inspector.
  250. This does not apply on tracks created with Godot versions prior to 3.4,
  251. as the animation reset track feature was added in 3.4.
  252. .. note:: RESET tracks is also used as a reference value for blending. See also `For better blending <../animation/animation_tree.html#for-better-blending>`__.