introduction.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. - Call functions with the powerful Call Function Tracks
  11. In Godot, you can animate anything available in the Inspector, such as
  12. Node transforms, sprites, UI elements, particles, visibility and color
  13. of materials, and so on. You can also modify values of script variables
  14. and call any function.
  15. Create an AnimationPlayer node
  16. ------------------------------
  17. To use the animation tools we first have to create an
  18. :ref:`class_AnimationPlayer` node.
  19. The AnimationPlayer node type is the data container for your animations.
  20. One AnimationPlayer node can hold multiple animations, that can
  21. automatically transition to one another.
  22. .. figure:: img/animation_create_animationplayer.png
  23. :alt: The AnimationPlayer node
  24. The AnimationPlayer node
  25. After creating one click on the AnimationPlayer node in the Node tab to
  26. open the Animation Panel at the bottom of the viewport.
  27. .. figure:: img/animation_animation_panel.png
  28. :alt: The animation panel position
  29. The animation panel position
  30. It consists of four parts:
  31. .. figure:: img/animation_animation_panel_overview.png
  32. :alt: The animation panel
  33. The animation panel
  34. - Animation controls (i.e. add, load, save, and delete animations)
  35. - The tracks listing
  36. - The timeline with keyframes
  37. - The timeline and track controls, where you can zoom the timeline and
  38. edit tracks for example.
  39. Computer animation relies on keyframes
  40. --------------------------------------
  41. A keyframe defines the value of a property at a certain point in time.
  42. Diamond shapes represent keyframes in the timeline. A line between two
  43. keyframes indicates that the value hasn't changed.
  44. .. figure:: img/animation_keyframes.png
  45. :alt: Keyframes in Godot
  46. Keyframes in Godot
  47. The engine interpolates values between keyframes, resulting in a gradual
  48. change in values 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 lets you insert keyframes and change their timing. It also
  53. defines how long the animation is.
  54. .. figure:: img/animation_timeline.png
  55. :alt: The timeline in the animation panel
  56. The timeline in the animation panel
  57. Each line of the Animation Panel is an animation track. Normal and
  58. Transform tracks reference node properties. Their name or id is a path
  59. to the node and the affected property.
  60. .. figure:: img/animation_normal_track.png
  61. :alt: Example of Normal animation tracks
  62. Example of Normal animation tracks
  63. .. tip::
  64. If you animate the wrong property, you can edit a track's path anytime.
  65. Double click on it and type the new path. Play the animation using the
  66. "Play from beginning" button |Play from beginning| (or pressing
  67. :kbd:`Shift + D` on keyboard) to see the changes instantly.
  68. Tutorial: Creating a simple animation
  69. -------------------------------------
  70. Scene setup
  71. ~~~~~~~~~~~
  72. For this tutorial, we'll create a Sprite node with an AnimationPlayer as
  73. its child. We will animate the sprite to move between two points on the screen.
  74. .. figure:: img/animation_animation_player_tree.png
  75. :alt: Our scene setup
  76. Our scene setup
  77. .. warning::
  78. AnimationPlayer inherits from Node instead of Node2D or Spatial, which means
  79. that the child nodes will not inherit the transform from the parent nodes
  80. due to a bare Node being present in the hierarchy.
  81. Therefore, it is not recommended to add nodes that have a 2D/3D transform
  82. as a child of an AnimationPlayer node.
  83. The sprite holds an image texture. We animate that sprite to move
  84. between two points on the screen. For this tutorial, use the default Godot
  85. icon as the sprite's texture. As a starting point, move the sprite
  86. to a left position on the screen.
  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. Triggers are a type of keyframe used by the
  158. ``current_animation`` property of a :ref:`class_AnimationPlayer`,
  159. and Animation Playback tracks.
  160. - **Capture:** If the first keyframe's time is greater than ``0.0``, the
  161. current value of the property will be remembered and
  162. will be blended with the first animation key. For example, you
  163. could use the Capture mode to move a node that's located anywhere
  164. to a specific location.
  165. .. figure:: img/animation_track_rate.png
  166. :alt: Track mode
  167. Track mode
  168. In normal animations, you usually use "Continuous". The other types are
  169. used to script complex animations.
  170. The interpolation tells Godot how to calculate the frame values between
  171. the keyframes. These interpolation modes are supported:
  172. - Nearest: Set the nearest keyframe value
  173. - Linear: Set the value based on a linear function calculation between
  174. the two keyframes
  175. - Cubic: Set the value based on a cubic function calculation between
  176. the two keyframes
  177. .. figure:: img/animation_track_interpolation.png
  178. :alt: Track interpolation
  179. Track interpolation
  180. Cubic interpolation leads to a more natural movement, where the
  181. animation is slower at a keyframe and faster between keyframes. This is
  182. usually used for character animation. Linear interpolation creates more
  183. of a robotic movement.
  184. Godot supports two loop modes, which affect the animation if it's set to
  185. loop:
  186. .. figure:: img/animation_track_loop_modes.png
  187. :alt: Loop modes
  188. Loop modes
  189. - Clamp loop interpolation: When this is selected, the animation stops
  190. after the last keyframe for this track. When the first keyframe is
  191. reached again, the animation will reset to its values.
  192. - Wrap loop interpolation: When this is selected, Godot calculates the
  193. animation after the last keyframe to reach the values of the first
  194. keyframe again.
  195. Keyframes for other properties
  196. ------------------------------
  197. Godot doesn't restrict you to only edit transform properties. Every
  198. property can be used as a track where you can set keyframes.
  199. If you select your sprite while the animation panel is visible, you get
  200. a small keyframe button for all the sprite's properties. Click on
  201. this button and Godot automatically adds a track and keyframe to the
  202. current animation.
  203. .. figure:: img/animation_properties_keyframe.png
  204. :alt: Keyframes for other properties
  205. Keyframes for other properties
  206. Edit keyframes
  207. --------------
  208. For advanced use and to edit keyframes in detail, You can click on them
  209. to bring up the keyframe editor in the inspector. You can use this to
  210. directly edit its values.
  211. .. figure:: img/animation_keyframe_editor_key.png
  212. :alt: Keyframe editor editing a key
  213. Keyframe editor editing a key
  214. Additionally, you can also edit the easing value for this keyframe by
  215. clicking and dragging the easing setting. This tells Godot, how to change
  216. the property values when it reaches this keyframe.
  217. You usually tweak your animations this way, when the movement doesn't
  218. "look right".
  219. Advanced: Call Method tracks
  220. ----------------------------
  221. Godot's animation engine doesn't stop here. If you're already
  222. comfortable with Godot's scripting language
  223. :ref:`doc_gdscript` and :doc:`/classes/index` you
  224. know that each node type is a class and has a bunch of callable
  225. methods.
  226. For example, the :ref:`class_AudioStreamPlayer` node type has a
  227. method to play an audio stream.
  228. Wouldn't it be great to use a method at a specific keyframe in an
  229. animation? This is where "Call Method Tracks" come in handy. These tracks
  230. reference a node again, this time without a reference to a property.
  231. Instead, a keyframe holds the name and arguments of a method, that
  232. Godot should call when it reaches this keyframe.
  233. To demonstrate, we're going to use a call method track to play audio at a
  234. specific keyframe. Normally to play audio you should use an audio track,
  235. but for the sake of demonstrating methods we're going to do it this way.
  236. Add a :ref:`class_AudioStreamPlayer` to the Scene Tree and setup a
  237. stream using an audio file you put in your project.
  238. Click on "Add track" (|Add track|) on the animation panel's track
  239. controls.
  240. Select "Add Call Method Track" from the list of possible track types.
  241. .. figure:: img/animation_add_call_method_track.png
  242. :alt: Add Call Method Track
  243. Add Call Method Track
  244. Select the :ref:`class_AudioStreamPlayer` node in the selection
  245. window. Godot adds the track with the reference to the node.
  246. .. figure:: img/animation_select_audiostreamplayer.png
  247. :alt: Select AudioStreamPlayer
  248. Select AudioStreamPlayer
  249. Right click the timeline where Godot should play the sample and
  250. click the "Insert Key" option. This will bring up a list of methods
  251. that can be called for the AudioStreamPlayer node. Select the first
  252. one.
  253. .. image:: img/animation_method_options.png
  254. When Godot reaches the keyframe, Godot calls the
  255. :ref:`class_AudioStreamPlayer` node's "play" function and the stream
  256. plays.
  257. You can change its position by dragging it on the timeline, you can also
  258. click on the keyframe and use the keyframe settings in the inspector.
  259. .. image:: img/animation_call_method_keyframe.png
  260. .. |Play from beginning| image:: img/animation_play_from_beginning.png
  261. .. |Add Animation| image:: img/animation_add.png
  262. .. |Add track| image:: img/animation_add_track.png
  263. Using RESET tracks
  264. ------------------
  265. You can set up a special *RESET* animation to contain the "default pose".
  266. This is used to ensure that the default pose is restored when you save
  267. the scene and open it again in the editor.
  268. For existing tracks, you can add an animation called "RESET" (case-sensitive),
  269. then add tracks for each property that you want to reset.
  270. The only keyframe should be at time 0, and give it the desired default value
  271. for each track.
  272. If AnimationPlayer's **Reset On Save** property is set to ``true``,
  273. the scene will be saved with the effects of the reset animation applied
  274. (as if it had been seeked to time ``0.0``).
  275. This only affects the saved file – the property tracks in the editor stay
  276. where they were.
  277. If you want to reset the tracks in the editor, select the AnimationPlayer node,
  278. open the **Animation** bottom panel then choose **Apply Reset** in the
  279. animation editor's **Animation** dropdown menu.
  280. When adding tracks on new animations, the editor will ask you to automatically
  281. create a RESET track when using the keyframe icon next to a property in the inspector.
  282. This does not apply on tracks created with Godot versions prior to 3.4,
  283. as the animation reset track feature was added in 3.4.