introduction.rst 12 KB

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