introduction.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. .. _doc_introduction_animation:
  2. Introduction to the 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. After creating one click on the AnimationPlayer node in the Node tab to
  28. open the Animation 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 four 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 timeline and track controls, where you can zoom the timeline and
  40. edit tracks for example.
  41. Computer animation relies on keyframes
  42. --------------------------------------
  43. A keyframe defines the value of a property at a certain point in time.
  44. Diamond shapes represent keyframes in the timeline. A line between two
  45. keyframes indicates that the value hasn't changed.
  46. .. figure:: img/animation_keyframes.png
  47. :alt: Keyframes in Godot
  48. Keyframes in Godot
  49. The engine interpolates values between keyframes, resulting in a gradual
  50. change in values over time.
  51. .. figure:: img/animation_illustration.png
  52. :alt: Two keyframes are all it takes to obtain a smooth motion
  53. Two keyframes are all it takes to obtain a smooth motion
  54. The timeline lets you insert keyframes and change their timing. It also
  55. defines how long the animation is.
  56. .. figure:: img/animation_timeline.png
  57. :alt: The timeline in the animation panel
  58. The timeline in the animation panel
  59. Each line of the Animation Panel is an animation track. Normal and
  60. Transform tracks reference node properties. Their name or id is a path
  61. to the node and the affected property.
  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 anytime.
  67. Double click on it and type 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 an AnimationPlayer node and a sprite node as its
  75. child.
  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. We animate that sprite to move
  86. between two points on the screen. For this tutorial, use the default Godot
  87. icon as the sprite's texture. As a starting point, move the sprite
  88. to a left position on the screen.
  89. Select the AnimationPlayer node, then click the "Animation" button in the
  90. animation editor. From the list select "New" (|Add
  91. Animation|) to add a new animation. And Enter a name for the animation in the
  92. dialog box.
  93. .. figure:: img/animation_create_new_animation.png
  94. :alt: Add a new animation
  95. Add a new animation
  96. Adding a track
  97. ~~~~~~~~~~~~~~
  98. To add a new track for our sprite, select it and take a look in the
  99. toolbar:
  100. .. figure:: img/animation_convenience_buttons.png
  101. :alt: Convenience buttons
  102. Convenience buttons
  103. These switches and buttons allow you to add keyframes for the selected
  104. node's location, rotation, and scale respectively.
  105. Deselect rotation, because we are only interested in the location of our
  106. sprite for this tutorial and click on the key button.
  107. As we don't have a track already set up for the transform/location
  108. property, Godot asks whether it should set it up for us. Click **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 second keyframe
  115. ~~~~~~~~~~~~~~~~~~~
  116. Now we need to set the destination where our sprite should be headed and
  117. how much time it takes to get there.
  118. Let's say, we want it to take 2 seconds to go to the other point. By
  119. default the animation is set to last only 1 second, so change this in
  120. the timeline controls in animation panel's lower panel to 2.
  121. .. figure:: img/animation_set_length.png
  122. :alt: Animation length
  123. Animation length
  124. Click on the timeline header near the 2-second mark and move the sprite
  125. to the target destination on the right side.
  126. Again, click the key button in the toolbar. This creates our second
  127. keyframe.
  128. Run the animation
  129. ~~~~~~~~~~~~~~~~~
  130. Click on the "Play from beginning" (|Play from beginning|) button.
  131. Yay! Our animation runs:
  132. .. figure:: img/animation_simple.gif
  133. :alt: The animation
  134. The animation
  135. Back and forth
  136. ~~~~~~~~~~~~~~
  137. Godot has an additional feature here. Like said before,
  138. Godot always calculates the frames between two keyframes. In a loop, the
  139. first keyframe is also the last keyframe, if no keyframe is specified at
  140. the end.
  141. .. figure:: img/animation_loop.png
  142. :alt: Animation loop
  143. Animation loop
  144. If you set the animation length to 4 seconds now, the animation moves
  145. back and forth. You can change this behavior if you change the track's
  146. loop mode. This is covered in the next chapter.
  147. Track settings
  148. ~~~~~~~~~~~~~~
  149. Each track has a settings panel at the end, where you can set the update
  150. mode, the track interpolation, and the loop mode.
  151. .. figure:: img/animation_track_settings.png
  152. :alt: Track settings
  153. Track settings
  154. The update mode of a track tells Godot when to update the property
  155. values. This can be:
  156. - Continuous: Update the property on each frame
  157. - Discrete: Only update the property on keyframes
  158. - Trigger: Only update the property on keyframes or triggers
  159. - Capture: The current value of a property is remembered, and it will blend with the first animation key found
  160. .. figure:: img/animation_track_rate.png
  161. :alt: Track mode
  162. Track mode
  163. In normal animations, you usually use "Continuous". The other types are
  164. used to script complex animations.
  165. The interpolation tells Godot how to calculate the frame values between
  166. the keyframes. These interpolation modes are supported:
  167. - Nearest: Set the nearest keyframe value
  168. - Linear: Set the value based on a linear function calculation between
  169. the two keyframes
  170. - Cubic: Set the value based on a cubic function calculation between
  171. the two keyframes
  172. .. figure:: img/animation_track_interpolation.png
  173. :alt: Track interpolation
  174. Track interpolation
  175. Cubic interpolation leads to a more natural movement, where the
  176. animation is slower at a keyframe and faster between keyframes. This is
  177. usually used for character animation. Linear interpolation creates more
  178. of a robotic movement.
  179. Godot supports two loop modes, which affect the animation if it's set to
  180. loop:
  181. .. figure:: img/animation_track_loop_modes.png
  182. :alt: Loop modes
  183. Loop modes
  184. - Clamp loop interpolation: When this is selected, the animation stops
  185. after the last keyframe for this track. When the first keyframe is
  186. reached again, the animation will reset to its values.
  187. - Wrap loop interpolation: When this is selected, Godot calculates the
  188. animation after the last keyframe to reach the values of the first
  189. keyframe again.
  190. Keyframes for other properties
  191. ------------------------------
  192. Godot doesn't restrict you to only edit transform properties. Every
  193. property can be used as a track where you can set keyframes.
  194. If you select your sprite while the animation panel is visible, you get
  195. a small keyframe button for all the sprite's properties. Click on
  196. this button and Godot automatically adds a track and keyframe to the
  197. current animation.
  198. .. figure:: img/animation_properties_keyframe.png
  199. :alt: Keyframes for other properties
  200. Keyframes for other properties
  201. Edit keyframes
  202. --------------
  203. For advanced use and to edit keyframes in detail, You can click on them
  204. to bring up the keyframe editor in the inspector. You can use this to
  205. directly edit its values.
  206. .. figure:: img/animation_keyframe_editor_key.png
  207. :alt: Keyframe editor editing a key
  208. Keyframe editor editing a key
  209. Additionally, you can also edit the easing value for this keyframe by
  210. clicking and dragging the easing setting. This tells Godot, how to change
  211. the property values when it reaches this keyframe.
  212. You usually tweak your animations this way, when the movement doesn't
  213. "look right".
  214. Advanced: Call Method tracks
  215. ----------------------------
  216. Godot's animation engine doesn't stop here. If you're already
  217. comfortable with Godot's scripting language
  218. :ref:`doc_gdscript` and :doc:`/classes/index` you
  219. know that each node type is a class and has a bunch of callable
  220. methods.
  221. For example, the :ref:`class_AudioStreamPlayer` node type has a
  222. method to play an audio stream.
  223. Wouldn't it be great to use a method at a specific keyframe in an
  224. animation? This is where "Call Method 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 method, that
  227. Godot should call when it reaches this keyframe.
  228. To demonstrate, we're going to use a call method track to play audio at a
  229. specific keyframe. Normally to play audio you should use an audio track,
  230. but for the sake of demonstrating methods we're going to do it this way.
  231. Add a :ref:`class_AudioStreamPlayer` to the Scene Tree and setup a
  232. stream using an audio file you put in your project.
  233. Click on "Add track" (|Add track|) on the animation panel's track
  234. controls.
  235. Select "Add Call Method Track" from the list of possible track types.
  236. .. figure:: img/animation_add_call_method_track.png
  237. :alt: Add Call Method Track
  238. Add Call Method Track
  239. Select the :ref:`class_AudioStreamPlayer` node in the selection
  240. window. Godot adds the track with the reference to the node.
  241. .. figure:: img/animation_select_audiostreamplayer.png
  242. :alt: Select AudioStreamPlayer
  243. Select AudioStreamPlayer
  244. Right click the timeline where Godot should play the sample and
  245. click the "Insert Key" option. This will bring up a list of methods
  246. that can be called for the AudioStreamPlayer node. Select the first
  247. one.
  248. .. image:: img/animation_method_options.png
  249. When Godot reaches the keyframe, Godot calls the
  250. :ref:`class_AudioStreamPlayer` node's "play" function and the stream
  251. plays.
  252. You can change its position by dragging it on the timeline, you can also
  253. click on the keyframe and use the keyframe settings in the inspector.
  254. .. image:: img/animation_call_method_keyframe.png
  255. .. |Play from beginning| image:: img/animation_play_from_beginning.png
  256. .. |Add Animation| image:: img/animation_add.png
  257. .. |Add track| image:: img/animation_add_track.png