<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eduardo Julião</title>
    <description>The latest articles on DEV Community by Eduardo Julião (@eduardojuliao).</description>
    <link>https://dev.to/eduardojuliao</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F660840%2F3b0261b2-b973-4193-8844-221e0c4a5f71.jpg</url>
      <title>DEV Community: Eduardo Julião</title>
      <link>https://dev.to/eduardojuliao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eduardojuliao"/>
    <language>en</language>
    <item>
      <title>Animated Sprite 2D</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Wed, 03 Jan 2024 00:52:16 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/animated-sprite-2d-147a</link>
      <guid>https://dev.to/eduardojuliao/animated-sprite-2d-147a</guid>
      <description>&lt;p&gt;Today's article is a simple one but a very powerful tool to have under your belt.&lt;/p&gt;

&lt;p&gt;We're going to create an animated coin collectible in our game, and to do that, we're going to explore the &lt;code&gt;AnimatedSprite2D&lt;/code&gt; node.&lt;/p&gt;

&lt;h2&gt;
  
  
  Source file
&lt;/h2&gt;

&lt;p&gt;For the rotating coin sprite, I'm using the &lt;a href="https://opengameart.org/content/rotating-coin"&gt;free coin asset&lt;/a&gt; that you can find in &lt;a href="//opengameart.org"&gt;Open Game Art&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YwSIbHQh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytsd5peos0mjkd3nwqwt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YwSIbHQh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytsd5peos0mjkd3nwqwt.png" alt="Image description" width="250" height="31"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Folder structure
&lt;/h2&gt;

&lt;p&gt;We're going to create two folders structure, one to store our sprites and other to store our items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res:///
├─ Objects/
│  ├─ Items/
│  │  ├─ Coin/
├─ Sprites/
│  ├─ Objects/
│  │  ├─ Items/
│  │  │   ├─ Coin/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;Objects&lt;/strong&gt; folder will contain our scenes and scripts while everything under the sprites folder will be, well, sprites.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Coin Scene
&lt;/h2&gt;

&lt;p&gt;Let's create a new Scene under the &lt;strong&gt;Coin&lt;/strong&gt; folder and name it &lt;code&gt;Coin.tscn&lt;/code&gt;, this will be a &lt;code&gt;Node2D&lt;/code&gt; scene.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u_CdfjWv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05m7j1t9tnw5cobethgd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u_CdfjWv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05m7j1t9tnw5cobethgd.png" alt="Image description" width="404" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding an &lt;code&gt;AnimatedSprite2D&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Right click the main Coin node, select &lt;strong&gt;Add child node&lt;/strong&gt; and search for &lt;strong&gt;AnimatedSprite2D&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sjstVI5S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dllhjejk98pjgnlsah2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sjstVI5S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dllhjejk98pjgnlsah2t.png" alt="Image description" width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our &lt;code&gt;AnimatedSprite2D&lt;/code&gt; node under our coin, let's add our Coin sprite to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding the Sprite to our &lt;code&gt;AnimatedSprite2D&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In the scene panel on the left side, select your &lt;code&gt;AnimatedSprite2D&lt;/code&gt; (it can be named as Sprite2D), and on the right side, in the inspector, select &lt;strong&gt;Sprite Frames &amp;gt; New Sprite Frames&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0O5MQRwg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq1invkk78sjh7bcjj1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0O5MQRwg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq1invkk78sjh7bcjj1j.png" alt="Image description" width="584" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will open tab in the bottom panel called &lt;strong&gt;Sprite Frames&lt;/strong&gt;, here is where we're going to import our coin sprite sheet.&lt;/p&gt;

&lt;p&gt;But before we add, let's "fix" the framerate to 8, because that's the number of frames our sprite sheet has.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I4URC49S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6qr78eibqvgkrg08gr8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I4URC49S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6qr78eibqvgkrg08gr8.png" alt="Image description" width="425" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, click on the icon on the right of the folder icon to import our spritesheet, it's called: &lt;strong&gt;Add frames from sprite sheet&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PDvwhK6w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwdach6smjgpf21yim9a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PDvwhK6w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwdach6smjgpf21yim9a.png" alt="Image description" width="761" height="109"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XENSJrNl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjrm26t76sy2ftfdn31l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XENSJrNl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjrm26t76sy2ftfdn31l.png" alt="Image description" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you click &lt;strong&gt;open&lt;/strong&gt; this window will appear, this will let us split our spritesheet into smaller sprites&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vwMuReTB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d66hlwt9o6bxryxjwfbg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vwMuReTB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d66hlwt9o6bxryxjwfbg.png" alt="Image description" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, there are vertical and horizontal lines, this tells the engine how the spritesheet should be split. Lets adjust these rows using the tools on the right side of this window.&lt;/p&gt;

&lt;p&gt;Since we have just wone row and 8 sprites, let's set Horizontal to 8 and Vertical to 1. Once you have your sprite sheet splitted, select the frames you want to add and press &lt;strong&gt;Add x Frames&lt;/strong&gt;, in our case, it should be 8.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oEoG8BPD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uh9jq9auhy92ounvwwwt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oEoG8BPD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uh9jq9auhy92ounvwwwt.png" alt="Image description" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can press play and you'll see the coin rotating in your scene! 😁&lt;/p&gt;

&lt;h2&gt;
  
  
  CONGRATULATIONS!
&lt;/h2&gt;

&lt;p&gt;🎉 Bravo on successfully completing this tutorial! Today's article might be simple, but the tool you've just added to your game development toolkit is undeniably powerful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you
&lt;/h2&gt;

&lt;p&gt;As always, thank you for reading and I hope you're enjoying it and learning from it!&lt;/p&gt;

&lt;p&gt;I see you, in the next article 😄&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn more
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.godotengine.org/en/stable/classes/class_animatedsprite2d.html"&gt;Animated Sprite 2D Official documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>godot</category>
      <category>gamedev</category>
      <category>tutorial</category>
      <category>gdscript</category>
    </item>
    <item>
      <title>Player camera</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Mon, 01 Jan 2024 02:13:49 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/player-camera-1ga9</link>
      <guid>https://dev.to/eduardojuliao/player-camera-1ga9</guid>
      <description>&lt;p&gt;Hello everyone! In today's article, I'm going to show you two ways you can make a camera follow the player character in Godot!&lt;/p&gt;

&lt;h2&gt;
  
  
  The easy way
&lt;/h2&gt;

&lt;p&gt;To make the camera follow the player is really easy, just create a camera node inside your player node and that's it! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NJjOBi6r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bz1jbv6xtmp18pegw467.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NJjOBi6r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bz1jbv6xtmp18pegw467.png" alt="Image description" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running the game in the current state, you'll notice that the player is now always in the center,  that's because the camera is following the player!&lt;/p&gt;

&lt;p&gt;Is that it? Is this really simple? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TqSyHzi0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b84fcc37znuh50rgvc9g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TqSyHzi0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b84fcc37znuh50rgvc9g.png" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As this works for prototyping, you'll hit some problems later in your game dev cycle, by making the camera inside your character, the character will need to know things about the world, which it doesn't actually need to know, like the size of the world you're in.&lt;/p&gt;

&lt;p&gt;To fix that, we're going the long hard way.&lt;/p&gt;

&lt;h2&gt;
  
  
  The long way
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Deleting the camera
&lt;/h3&gt;

&lt;p&gt;We need to delete the camera we created inside our player node, so we can start creating a new one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Organizing the code
&lt;/h3&gt;

&lt;p&gt;We're going to create a new folder inside the Player folder, called it camera! All the code and nodes related to the player camera will be stored here.&lt;/p&gt;

&lt;p&gt;The project strucutre should look like this now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res:///
├─ Characters/
│  ├─ Player/
│  │   ├─ Camera/
├─ Scenes/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  RemoteTransform2D
&lt;/h3&gt;

&lt;p&gt;Before we start developing our camera, we need to attach a &lt;a href="https://docs.godotengine.org/en/stable/classes/class_remotetransform2d.html"&gt;&lt;code&gt;RemoteTransform2D&lt;/code&gt;&lt;/a&gt;.&lt;br&gt;
This is a very cool node! It pushes its position into another node that you tell it to, in our case, the player camera.&lt;/p&gt;

&lt;p&gt;So click on your player node and add a new node of type &lt;code&gt;RemoteTransform2D&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wTybCXke--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezixp2df6mkojsemw6u8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wTybCXke--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezixp2df6mkojsemw6u8.png" alt="Image description" width="800" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll notice that the node shows a warning, no need to worry, because we're going to set what this note will send signals to dynamically on our camera script!&lt;/p&gt;
&lt;h3&gt;
  
  
  Player camera
&lt;/h3&gt;

&lt;p&gt;We're all set to start developing our camera, so lets get to it.&lt;/p&gt;
&lt;h4&gt;
  
  
  Create the scene
&lt;/h4&gt;

&lt;p&gt;Now that our folders are nice and organized, let's go ahead and create our player camera, really simple!&lt;/p&gt;

&lt;p&gt;Let's start by create a &lt;code&gt;PlayerCamera&lt;/code&gt; scene into the Player/Camera folder! But this time, select &lt;strong&gt;other node&lt;/strong&gt; and when prompted to select the node type, look for &lt;strong&gt;Camera2D&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VkeTc2Ag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m153lqckhg48rv7kenik.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VkeTc2Ag--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m153lqckhg48rv7kenik.png" alt="Image description" width="400" height="339"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Camera Script
&lt;/h4&gt;

&lt;p&gt;Right click the PlayerCamera node and &lt;strong&gt;Attach a script&lt;/strong&gt; to it!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9wWKz7p5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsb7wcx8htvkti06q6qo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9wWKz7p5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsb7wcx8htvkti06q6qo.png" alt="Image description" width="462" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For now, the script will be really simple, but we're going to revisit it when we dynamically instantiate the player into the scene.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;PlayerCamera.gd&lt;/code&gt;, start by create a &lt;code&gt;func ready():&lt;/code&gt;, in there we're going to find the player in the current scene and attach to our camera.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_ready&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# This script is used to set the remote path of the player's RemoteTransform2D
&lt;/span&gt;    &lt;span class="c1"&gt;# so that the player's position is synced across the nodes.
&lt;/span&gt;    &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;playerRemoteTransform2d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_tree&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;current_scene&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Player/RemoteTransform2D&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;playerRemoteTransform2d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remote_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_path&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's analyze whats happening here. &lt;/p&gt;

&lt;h5&gt;
  
  
  Getting the player node in the scene
&lt;/h5&gt;

&lt;p&gt;The first line we're getting the &lt;code&gt;RemoteTransform2D&lt;/code&gt; inside our &lt;code&gt;Player&lt;/code&gt; by using &lt;code&gt;get_node(...)&lt;/code&gt;, inside our &lt;code&gt;current_scene&lt;/code&gt; that is in our tree of nodes &lt;code&gt;get_tree()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's somethig like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZgDZaZaB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym5ipdqxvpq514n85rap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZgDZaZaB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym5ipdqxvpq514n85rap.png" alt="Image description" width="482" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you run your project, in the scene tab you can change to Remote and find all nodes that are currently running, when you have nodes that are singleton/auto loads, they'll show outside the scene and inside the tree.&lt;/p&gt;

&lt;h5&gt;
  
  
  Attaching the camera to the RemoteTransform2d
&lt;/h5&gt;

&lt;p&gt;The second line, sets the &lt;code&gt;remote_path&lt;/code&gt; of the &lt;code&gt;Player/RemoteTransform2D&lt;/code&gt; to our player camera, to do that we use the &lt;code&gt;get_path()&lt;/code&gt; method, which will return the path of the current node.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating our Scene!
&lt;/h2&gt;

&lt;p&gt;We have everything we need for our camera to follow our player around!&lt;br&gt;
Now, let's add our &lt;code&gt;PlayerCamera&lt;/code&gt; scene into our &lt;code&gt;FirstScene&lt;/code&gt;, you can do so by dragging in dropping the &lt;code&gt;PlayerCamera&lt;/code&gt; into the scene main node.&lt;/p&gt;

&lt;p&gt;Also, to get a sense our player is moving, Drag and dro the &lt;code&gt;icon.svg&lt;/code&gt; (the godot icon) and set the modulate to another color, this will make it easier to distinguish between this image and the player.&lt;/p&gt;

&lt;p&gt;To do that, click on the icon node inside the scene, navigate to &lt;strong&gt;Canvas Item&lt;/strong&gt; and then modulate, you can give the color you want here, I gave i a light green.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1NZES3L1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dv48glwod7u586yjnru1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1NZES3L1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dv48glwod7u586yjnru1.png" alt="Image description" width="586" height="658"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your scene should look something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1LAoBtfT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y35vpdcdtd3pmdqworm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1LAoBtfT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y35vpdcdtd3pmdqworm.png" alt="Image description" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now run your project and you can see the player moving and your camera following it! How cool is that?&lt;/p&gt;

&lt;h2&gt;
  
  
  Congratulations!
&lt;/h2&gt;

&lt;p&gt;We learn a lot in this series and we can see our project taking shape! Now that we have a functional player and camera, we can start building the world around it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you
&lt;/h2&gt;

&lt;p&gt;As always, thank you for reading and I hope you're enjoying it and learning from it!&lt;/p&gt;

&lt;p&gt;I see you, in the next article 😄&lt;/p&gt;

</description>
      <category>godot</category>
      <category>gamedev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Make your player roll and animations!</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Sat, 30 Dec 2023 23:52:21 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/make-your-player-roll-and-animations-4ch8</link>
      <guid>https://dev.to/eduardojuliao/make-your-player-roll-and-animations-4ch8</guid>
      <description>&lt;p&gt;In today's article, I'm going to show you how to make your player roll, manage states and basics of animation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a new action in the Input Map
&lt;/h2&gt;

&lt;p&gt;Before we start writing our code, let's create a new action into our &lt;code&gt;Input Map&lt;/code&gt; named &lt;strong&gt;roll&lt;/strong&gt;. I've attached the letter &lt;code&gt;K&lt;/code&gt; on the keyboard, but you can bind any key you prefer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ocruvg9m4u22piwyo5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ocruvg9m4u22piwyo5o.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding a new state
&lt;/h2&gt;

&lt;p&gt;Open the &lt;code&gt;Player.gd&lt;/code&gt; script and lets update our &lt;code&gt;states&lt;/code&gt; enum, adding the action &lt;code&gt;ROLL&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;states&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;MOVE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;ROLL&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using &lt;code&gt;_input(event)&lt;/code&gt; to change states
&lt;/h2&gt;

&lt;p&gt;In the previous article I introduced the &lt;code&gt;_input(event)&lt;/code&gt; method, now, we're going to use it to change the player state based on the player input!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;## Called when player input is detected.
## @param event: InputEvent
## @return void
&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;InputEventMouseButton&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;InputEventMouseMotion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;# Only process input if the player is in the MOVE state.
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;_current_state&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MOVE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# If the player presses the roll button, then the player should roll.
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_action_pressed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;roll&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="c1"&gt;# sets the _current_state to states.ROLL
&lt;/span&gt;            &lt;span class="n"&gt;_current_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ROLL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we're going to check if the player is in the &lt;code&gt;MOVE&lt;/code&gt; state, this will prevent the player from rolling mid attack, or attack mid rolling.&lt;/p&gt;

&lt;p&gt;Then, we check if the player pressed what is mapped to the &lt;code&gt;roll&lt;/code&gt;, if so, we change the current state to &lt;code&gt;ROLL&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating player movement
&lt;/h2&gt;

&lt;p&gt;Now that we changed our state to &lt;code&gt;ROLL&lt;/code&gt;, we need to tell the script what to do when the player is in this state.&lt;/p&gt;

&lt;p&gt;Let's start by creating a &lt;code&gt;roll&lt;/code&gt; function, like we created with the &lt;code&gt;move(delta)&lt;/code&gt;. And as we did in the &lt;code&gt;move(delta)&lt;/code&gt; function, we'll create a new variable, the &lt;code&gt;_roll_speed&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@export&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_roll_speed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we update the &lt;code&gt;_physics_process(delta)&lt;/code&gt; function to add the call to the roll function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# delta = time between frames
&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_physics_process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="n"&gt;_current_state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MOVE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ROLL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;roll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The editor will throw you an error since we don't have a &lt;code&gt;roll()&lt;/code&gt; function created, so let's scroll all the way down on our script and create a new function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;roll&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_roll_speed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;move_and_slide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But wait, we can't simply say the velocity is the &lt;code&gt;_roll_speed&lt;/code&gt;, we need a vector, just as we used the &lt;code&gt;input_vector&lt;/code&gt; in our &lt;code&gt;move(delta)&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So let's create a new variable that will receive the input vector and we can use in our roll function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_roll_vector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Vector2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RIGHT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a &lt;code&gt;_roll_vector&lt;/code&gt; variable we can use and will initialise its value to right.&lt;/p&gt;

&lt;p&gt;Now, our &lt;code&gt;roll()&lt;/code&gt; function will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;roll&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_roll_vector&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;_roll_speed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;move_and_slide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we can't forget to update our &lt;code&gt;move(delta)&lt;/code&gt; function to set our new &lt;code&gt;_roll_vector&lt;/code&gt;. Let's add the following code before we update our velocity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This will be used in the roll() function.
# This will be used to determine which direction the player should roll.
&lt;/span&gt;&lt;span class="n"&gt;_roll_vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And why we update only when the player moves? Because this will keep the player "facing diretion", so if the player decides to walk to the left, we store this "last facing direction" to our &lt;code&gt;_roll_vector&lt;/code&gt;, if the player stops, this variable will keep this state, when the player rolls, it will roll to the left.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the player
&lt;/h2&gt;

&lt;p&gt;We're ready to run our game! But notice one thing, if we roll, the player keeps going, and going and we can't stop it! But why this happens?&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;_input(event)&lt;/code&gt; function we only accept input when the player state is &lt;code&gt;MOVE&lt;/code&gt;, but we don't have anything telling the script to revert to the &lt;code&gt;MOVE&lt;/code&gt; state.&lt;/p&gt;

&lt;p&gt;To solve this issue, we're going to use animations!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the animation node.
&lt;/h2&gt;

&lt;p&gt;Let's get back to our player node in Godot and add two new nodes &lt;code&gt;AnimationPlayer&lt;/code&gt; and &lt;code&gt;AnimationTree&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flvu2w2fp43a21ewcz8es.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flvu2w2fp43a21ewcz8es.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the &lt;code&gt;AnimationTree&lt;/code&gt; have a warning sign next to it, this is because it needs to be told how it should handle animations. Select the &lt;code&gt;AnimationTree&lt;/code&gt; node and on the right side, in the inspector, and update the &lt;code&gt;Tree Root&lt;/code&gt; to use &lt;code&gt;AnimationNodeStateMachine&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The next step, is to assign the &lt;code&gt;Anim Player&lt;/code&gt; to the animation player we created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk3vvzxoesuquqm47u5x0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk3vvzxoesuquqm47u5x0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will open an &lt;code&gt;AnimationTree&lt;/code&gt; window on the bottom of Godot, we're going to create a couple of animations (even though they're just the godot icon 😅), and update the animations through our script! &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating new animations
&lt;/h2&gt;

&lt;p&gt;First, select the &lt;code&gt;Animation&lt;/code&gt; tab in the bottom panel, then, in new panel, click on &lt;strong&gt;Animation&lt;/strong&gt; at the top and then select the option &lt;strong&gt;New&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb9i6o4qdsme2sdjla7o0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb9i6o4qdsme2sdjla7o0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's create animations for idle, walk and roll. Each of them having the facing direction, for example, idle_left, idle_right, so on.&lt;/p&gt;

&lt;p&gt;You should have something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3gbazv9uz5bs99knspb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3gbazv9uz5bs99knspb.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understaing The animation window
&lt;/h2&gt;

&lt;p&gt;Let's start by updating the &lt;code&gt;roll_right&lt;/code&gt; animation, select it on the drop down and the first thing we're going to do, is to add our sprite to it!&lt;br&gt;
To do this, keep the animation window open, select the &lt;code&gt;Sprite2D&lt;/code&gt; node in the scene and under &lt;strong&gt;Animation&lt;/strong&gt; in the inspector, select frame 0 and press the key icon to the right of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4v86cswon23di4asx1i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl4v86cswon23di4asx1i.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqc8elar7m3sbb2ten19y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqc8elar7m3sbb2ten19y.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will open a new window, just press create.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynhwh1g1pt7vwu2uuwvu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynhwh1g1pt7vwu2uuwvu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will add our sprite to the tracks!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc7xign4kzwy63wypjbw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc7xign4kzwy63wypjbw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cool! we have our first track with our sprite! But before continuing, let's update some options in the window, to make it easier for us to work.&lt;/p&gt;

&lt;p&gt;In bottom right of the animation window, change from seconds to FPS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuge3frb86bjxjm74n75c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuge3frb86bjxjm74n75c.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is where we tell how many frames the animation will have, for the roll animation, lets pretend we have 4 frames, so let's update the value on the left to 4!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3dsic7tvqw6e8gi7i7jt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3dsic7tvqw6e8gi7i7jt.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you press the play button in the animation window, you'll notice that the track will stop on frame 4 (it counts 0,1,2,3), signaling the engine that the animation ended.&lt;/p&gt;
&lt;h2&gt;
  
  
  Adding a &lt;code&gt;Call Method Track&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Call Method Track&lt;/strong&gt; lets you call a methoid in a script at anypoint of the animation you want! Which is great! because we're going to use this to change the state of our player.&lt;/p&gt;

&lt;p&gt;CLick on the &lt;strong&gt;+ Add Track&lt;/strong&gt; and select &lt;strong&gt;Call Method Track&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8x2tjm7hgxpt9nrtkfj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8x2tjm7hgxpt9nrtkfj8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will open a new window for you to select a node to animate, in this window, select the player. We're selecting the player node becuase we're adding the script there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjbiy2nhjm974m9l9av50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjbiy2nhjm974m9l9av50.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you're done, this will create a new track in the Animation window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2elbqyax0pqj01wyeh9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2elbqyax0pqj01wyeh9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get back to our &lt;code&gt;Player.gd&lt;/code&gt; and create a simple function that will set the state back to move, let's call it &lt;code&gt;on_roll_ended()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;on_roll_ended&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;_current_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MOVE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to add your script, make sure the blue vertical bar is at the last frame of your animation (in this case, the end of the third frame beginning of the forth), right click it and press &lt;strong&gt;Add Key&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the new window, select the method we just created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F13htawm0paxz1z0745w4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F13htawm0paxz1z0745w4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will tell the Engine that when the roll_right animation ends, it should call the method we assigned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8g4ywfxlwotf0349gbt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8g4ywfxlwotf0349gbt.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we continue, do this for all roll animations 😄.&lt;/p&gt;

&lt;h2&gt;
  
  
  The animation Tree
&lt;/h2&gt;

&lt;p&gt;We're almost done, we need to set up the animations in the animation tree so we can use it on our scripts.&lt;/p&gt;

&lt;p&gt;Select both animation tree under the player node and in the bottom panel.&lt;/p&gt;

&lt;p&gt;You'll see a Start/End blocks, we're going to create a few more, one for idle, one for walk and one for roll. Right click anywhere between Start and End and select the &lt;strong&gt;Add Blend Space 2D&lt;/strong&gt; option, once it's created, double click the text and rename it to &lt;strong&gt;Idle&lt;/strong&gt;, do the same for walk and roll.&lt;/p&gt;

&lt;p&gt;Now, let's link these aniumations together! &lt;br&gt;
There are a couple of tool in to top left corner of the Animation tree&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fskkc87iqf1hbhh2kdm3s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fskkc87iqf1hbhh2kdm3s.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From left to right:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select Tool
With this selected, you can click on any node in the animation tree.&lt;/li&gt;
&lt;li&gt;Add Tool
When this is active, clicking anywhere in the animation tree with the left mouse button will open the add pop-up.&lt;/li&gt;
&lt;li&gt;Connect Tool
When Connect Tool is selected, you can link animations together.&lt;/li&gt;
&lt;li&gt;Delete Tool
Deletes the selected node.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Select the Connect Tool and let's start linkin them. To link animations, click on the node you want to link and drag to the next node.&lt;/p&gt;

&lt;p&gt;You should have something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffckqg8mysi5np36tkv76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffckqg8mysi5np36tkv76.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we proceed, schange to the select tool and select the lines between nodes, now in the inspector, navigate to &lt;strong&gt;Advance&lt;/strong&gt; and change the &lt;strong&gt;Mode&lt;/strong&gt; property to &lt;strong&gt;Enabled&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgoldw5ki95dy81hcdgfv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgoldw5ki95dy81hcdgfv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do this to all connections!&lt;br&gt;
One last step!! (I swear, it's almost at the end) press the pencil icon in the &lt;code&gt;Roll&lt;/code&gt; node, this will open a window with the details to the Roll (or any animation you pressed) animation.&lt;br&gt;
Let's add our animations we created in the &lt;code&gt;Animation&lt;/code&gt; window here, and we're ready to use them!&lt;/p&gt;

&lt;p&gt;First, change the blend mode to the tree dots here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw12rh9j6nmyw7irywfsz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw12rh9j6nmyw7irywfsz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, select the select tool in the top left corner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnflfasmcsz72ltzr5rk2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnflfasmcsz72ltzr5rk2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right click as close as you can to the 0 in the left of the graph &amp;gt; add animation &amp;gt; roll_left. Now, when we set the blend position in our script, it'll update the animation based on the X and Y positions. Do this for the remaining animations!&lt;br&gt;
At the end, you will have something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1zliw8wmaz5wy8a74ss3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1zliw8wmaz5wy8a74ss3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  !! IMPORTANT !!
&lt;/h3&gt;

&lt;p&gt;in Godot, up = -1 and down = 1, that's the reason we add the down animation up and up animation down.&lt;/p&gt;
&lt;h2&gt;
  
  
  Updating animations in the script
&lt;/h2&gt;

&lt;p&gt;This is our last step, calling different animations in our script! Open the player script and let's start working on calling these animations!&lt;/p&gt;

&lt;p&gt;Open the Player Script, first things first, lets get our nodes in the script, we can do so like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@onready&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;animationTree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;AnimationTree&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nd"&gt;@onready&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;animationState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;animationTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parameters/playback&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a couple of new keywords here, let's explore them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;@onready 
Tells the engine that this variable will be loaded once the player (or the node) is loaded into the scene tree.&lt;/li&gt;
&lt;li&gt;$AnimationTree 
having the &lt;code&gt;$&lt;/code&gt; sign is a syntax sugar that would be the same as calling &lt;code&gt;get_node("AnimationTree")&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Enable the AnimationTree
&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;_ready()&lt;/code&gt; function, add the following code, this will enable the animation tree once the player is ready.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;animationTree.active = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setting up the blend position
&lt;/h2&gt;

&lt;p&gt;In this step, is very important to use the exact names in the animation tree, otherwise you'll get some errors in the run time.&lt;/p&gt;

&lt;p&gt;The recipe to set the blend position is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;animationTree.set("parameters/&amp;lt;Animation_Name&amp;gt;/blend_position", &amp;lt;movement_vector&amp;gt;);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's add the following code in the &lt;code&gt;_physics_process&lt;/code&gt; function. First, the idle animation, just beneath the &lt;code&gt;if input_vector == Vector2.ZERO:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;animationTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parameters/Idle/blend_position&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for walk and roll animation we can set after the &lt;code&gt;_roll_vector = input_vector;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;_roll_vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;animationTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parameters/Walk/blend_position&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;animationTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parameters/Roll/blend_position&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Calling animations
&lt;/h2&gt;

&lt;p&gt;In this last step, we need to tell the animation state which animation should be playing.&lt;/p&gt;

&lt;p&gt;Let's call our animations, &lt;code&gt;Idle&lt;/code&gt;, &lt;code&gt;Walk&lt;/code&gt; and &lt;code&gt;Roll&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code is really simple, just use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# For Idle
animationState.travel("Idle");

# For Walk
animationState.travel("Walk");

# For Roll 
animationState.travel("Roll");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the Idle, call it after setting the Idle &lt;code&gt;blend_position&lt;/code&gt;. For Walk, call it after setting the Walk &lt;code&gt;blend_position&lt;/code&gt; and for Roll, call it in the &lt;code&gt;roll()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ZERO&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;animationTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parameters/Idle/blend_position&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;animationState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;travel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Idle&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;_roll_vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;animationTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parameters/Walk/blend_position&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;animationTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;parameters/Roll/blend_position&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;animationState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;travel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Walk&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func roll():
    velocity = _roll_vector * _roll_speed;
    animationState.travel("Roll");
    move_and_slide();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CONGRATULATIONS!
&lt;/h2&gt;

&lt;p&gt;Phew, that was a long one, but now you now how powerful the animation can be in Godot! &lt;br&gt;
Now, when you run the game, once you press the Roll action, your player will roll to the last facing position!&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you!
&lt;/h2&gt;

&lt;p&gt;I hope you're enjoying this series as much as I'm enjoying writing them! &lt;/p&gt;

&lt;p&gt;Thank you for reading it, any problems or questions drop a comment below and I'll get to it!&lt;/p&gt;

&lt;p&gt;Thanks again, and I'll see you in the next article! &lt;/p&gt;

</description>
      <category>godot</category>
      <category>gamedev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Player Character - First steps (literally)</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Sat, 30 Dec 2023 01:26:44 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/player-character-first-steps-literally-1agm</link>
      <guid>https://dev.to/eduardojuliao/player-character-first-steps-literally-1agm</guid>
      <description>&lt;p&gt;Today I'm going to show you how to make your player take its first steps! &lt;/p&gt;

&lt;p&gt;To achieve this, we're going to write some code in &lt;a href="https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html"&gt;GDScript&lt;/a&gt;, Godot native language.&lt;/p&gt;

&lt;p&gt;You can use other languages with Godot, such as C++ and C#, but this series will use GDScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Player basics
&lt;/h2&gt;

&lt;p&gt;Let's rename our &lt;code&gt;CharacterBody2D&lt;/code&gt; to &lt;strong&gt;Player&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Collision shape
&lt;/h3&gt;

&lt;p&gt;In the previous article, I mentioned that the &lt;code&gt;CharacterBody2D&lt;/code&gt; needs a collision shape node. We're going to add one, and it's really easy to do so.&lt;/p&gt;

&lt;p&gt;Right click our player node and select the &lt;strong&gt;+ Add Child Node&lt;/strong&gt; option, in the newly open window, search for &lt;code&gt;CollisionShape2D&lt;/code&gt; select it and hit &lt;strong&gt;Create&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1hCR21t4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/duhkt8pq906524syhv5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1hCR21t4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/duhkt8pq906524syhv5p.png" alt="Image description" width="285" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Another warning? 🙄
&lt;/h4&gt;

&lt;p&gt;We've added a collision shape 2d node, but we need to specify what is the shape of this collision! To do this, select the &lt;code&gt;CollisionShape2D&lt;/code&gt; node and in the inspector (right side panel), expand the &lt;code&gt;Shape&lt;/code&gt; options and select &lt;strong&gt;RectangleShape2D&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZVbVGqs9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8aclk00v1sty11jzzf1p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZVbVGqs9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8aclk00v1sty11jzzf1p.png" alt="Image description" width="586" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Know error
&lt;/h4&gt;

&lt;p&gt;Note that we added a collision node but it appears in a different position than our sprite:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KqUznNvG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvyxy96qb9p2jg6v0il7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KqUznNvG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvyxy96qb9p2jg6v0il7.png" alt="Image description" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is happening here is that the collision shape is using the position of our Player node while the sprite position is different (we moved in the preivous article).&lt;/p&gt;

&lt;p&gt;To fix this issue, first, let's move our &lt;code&gt;Sprite2D&lt;/code&gt; back to the starting point of our scene, that being x = 0, y = 0.&lt;/p&gt;

&lt;p&gt;Select the &lt;code&gt;Sprite2D&lt;/code&gt; node, expand the &lt;code&gt;transform&lt;/code&gt; option and set position x and y to 0.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ySuYdcT5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuq86lftwgir8elbb0gv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ySuYdcT5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuq86lftwgir8elbb0gv.png" alt="Image description" width="582" height="778"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will bring the sprite to the starting position.&lt;/p&gt;

&lt;h3&gt;
  
  
  Collision Size
&lt;/h3&gt;

&lt;p&gt;Now, let's set the collision format properly.&lt;/p&gt;

&lt;p&gt;Select the &lt;code&gt;CollisionShape2D&lt;/code&gt;, you're going to notice a small blue square in the center of our sprite&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EgtOyKLe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgr8gmlrktsqbo1gqgk8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EgtOyKLe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgr8gmlrktsqbo1gqgk8.png" alt="Image description" width="710" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can change the color of this shape (for debug purporses), to do this, select the &lt;code&gt;CollisionShape2D&lt;/code&gt; and change the Debug Color to the color your heart desires, I used a yellow-ish tone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gt2jQ7cF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p87pimrf9qkspkml9ok2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gt2jQ7cF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p87pimrf9qkspkml9ok2.png" alt="Image description" width="586" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, resize the Collision shape so it's the same size as the sprite.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GwTQIgsS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6dbo3ewkw3uxb8y3fth.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GwTQIgsS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6dbo3ewkw3uxb8y3fth.png" alt="Image description" width="778" height="657"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice! We have added a collision node to our player.&lt;/p&gt;

&lt;p&gt;What about we make our player move around?&lt;/p&gt;

&lt;h3&gt;
  
  
  Folders
&lt;/h3&gt;

&lt;p&gt;Let's keep our project organised, I like to have a folder for my characters (be them player, Npcs, enemies).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res://
├─ Characters/
│  ├─ Player/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yPlu160D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smiu6b10p1ps6wx2qizl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yPlu160D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smiu6b10p1ps6wx2qizl.png" alt="Image description" width="288" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this folder we're going to store the player scene and scripts. Since we already have a node called player in our scene, we can save this as our player.&lt;/p&gt;

&lt;p&gt;To save our player as a scene (don't worry, a scene is like a prefab), right click our player node and select the option &lt;strong&gt;Save branch as scene&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cg8cVShw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srbjid83vkp8ce5cdu79.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cg8cVShw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srbjid83vkp8ce5cdu79.png" alt="Image description" width="463" height="702"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save it under our Player folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DNLGB-YB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9kx4xrlgsffae0b9ox8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DNLGB-YB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9kx4xrlgsffae0b9ox8.png" alt="Image description" width="281" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that our Player node in the scene is now just a "simple" node.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ylYMPn79--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvl3t4v0quhyag0qdz3u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ylYMPn79--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvl3t4v0quhyag0qdz3u.png" alt="Image description" width="284" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't worry, everything related to our player (sprites, animations, scripts, etc) are packed into this Node.&lt;/p&gt;

&lt;p&gt;Now, double click our Player scene in the FileSystem to open our Player Scene.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wk4l7Fpc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ixzf5k7uon22g5mqpu6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wk4l7Fpc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ixzf5k7uon22g5mqpu6.png" alt="Image description" width="658" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See, all our nodes are there 😉&lt;/p&gt;

&lt;h3&gt;
  
  
  Project settings - input mapping
&lt;/h3&gt;

&lt;p&gt;Before we start coding our player, we need to tell the Engine what types of input we want and how we want it.&lt;/p&gt;

&lt;p&gt;Select &lt;strong&gt;Project &amp;gt; Project Settings&lt;/strong&gt; in the top men&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OG0jAjFC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uniu43brwj48c6s8oxuu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OG0jAjFC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uniu43brwj48c6s8oxuu.png" alt="Image description" width="486" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;selet the &lt;strong&gt;Input Map&lt;/strong&gt; tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z0R4ii7X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esajgtl9vda2cql3twjc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z0R4ii7X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esajgtl9vda2cql3twjc.png" alt="Image description" width="800" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Adding an input map
&lt;/h4&gt;

&lt;p&gt;Now lets get started with telling the engine how we want our inputs to work. &lt;br&gt;
Select the &lt;strong&gt;Add New Action&lt;/strong&gt; text field, write &lt;code&gt;move_up&lt;/code&gt;  and hit &lt;strong&gt;add&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oErgGEu_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8klwc8ytq8d9187dtimd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oErgGEu_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8klwc8ytq8d9187dtimd.png" alt="Image description" width="800" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will create a new action for us.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4qJnIMq0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10a5uw0qtv9py9ez4xns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4qJnIMq0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10a5uw0qtv9py9ez4xns.png" alt="Image description" width="800" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to tell to the Engine which buttons will trigger this.&lt;/p&gt;

&lt;p&gt;Press the &lt;strong&gt;+&lt;/strong&gt; sign by the side of &lt;code&gt;move_up&lt;/code&gt;, a new window will open up, in there, type W (or the key you want to make the player go up) and hit ok! &lt;/p&gt;

&lt;p&gt;Note that you can add multiple inputs for this, like W, up arrow keys, up on a controller etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xPY_eWWj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cwrnuem7trk7ba4dp3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xPY_eWWj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cwrnuem7trk7ba4dp3i.png" alt="Image description" width="800" height="494"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do the same for all other movements: move_right, move_down, move_left. You should have something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8GKDkoAS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mk5ztpwiwxa1hax8r8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8GKDkoAS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mk5ztpwiwxa1hax8r8t.png" alt="Image description" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Close this window and we're done!&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating the player script
&lt;/h3&gt;

&lt;p&gt;The section we've all been waiting for! Creating a script!&lt;br&gt;
Right click our Player node, and select the option &lt;strong&gt;Attach Script&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mUfYZW_P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z11kmjd58ctz1lrgojhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mUfYZW_P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z11kmjd58ctz1lrgojhg.png" alt="Image description" width="437" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure the script is under the same folder and hit &lt;strong&gt;Create&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ukpqpCFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz06jdo6e5qqk3fpmk17.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ukpqpCFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz06jdo6e5qqk3fpmk17.png" alt="Image description" width="462" height="353"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Important functions
&lt;/h3&gt;

&lt;p&gt;We're going to use a couple of functions to make our player move around.&lt;/p&gt;
&lt;h4&gt;
  
  
  _ready()
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;_ready&lt;/code&gt; is called once the player is added to the scene, we're going to use this function to set the innitial state of our player.&lt;/p&gt;
&lt;h4&gt;
  
  
  _input(event)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;_input(event)&lt;/code&gt; is called when the engine receives an input (mapped or not), such as keyboard key presses, mouse movement, joypad button press and many others.&lt;br&gt;
We're going to use this function to know what was pressed and change the player state.&lt;/p&gt;
&lt;h5&gt;
  
  
  Parameter: event
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;event&lt;/code&gt; is the event that it was pressed. You can read more on &lt;code&gt;InputEvents&lt;/code&gt; from the &lt;a href="https://docs.godotengine.org/en/stable/tutorials/inputs/inputevent.html#doc-inputevent"&gt;official documentation&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  _process_physics(delta)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;_process_physics(delta)&lt;/code&gt; is different than &lt;code&gt;_process(delta)&lt;/code&gt;. &lt;code&gt;_process&lt;/code&gt; is called every frame while &lt;code&gt;_process_physics&lt;/code&gt; is called at a fixed frame rate, being 60 frame per seconds by default, this is great for moving the player.&lt;/p&gt;
&lt;h5&gt;
  
  
  Parameter: delta
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;delta&lt;/code&gt; is the time, in seconds, since the previous call&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating the player possible states
&lt;/h3&gt;

&lt;p&gt;Our player will be able to walk, attack, interact and many others, so let's create an &lt;code&gt;enum&lt;/code&gt; to keep track of there states.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;MOVE&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_current_state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;_ready&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;_current_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MOVE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above script I create an &lt;code&gt;enum&lt;/code&gt; to hold the player states, a variable to store the current player state and in the &lt;code&gt;_ready()&lt;/code&gt; function I'm setting the innitial state to &lt;code&gt;MOVE&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling input
&lt;/h3&gt;

&lt;p&gt;For now, we will simply return, and we will revisit this function when we have more actions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Processing player movement
&lt;/h3&gt;

&lt;p&gt;To start calculating the player movement, we're going to create a couple more variables, &lt;code&gt;acceleration&lt;/code&gt;, &lt;code&gt;friction&lt;/code&gt; and &lt;code&gt;run speed&lt;/code&gt;, and use the function &lt;code&gt;_process_physics(delta)&lt;/code&gt; to update the player position based on those properties&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_run_speed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_acceleration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_friction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add the &lt;code&gt;@export&lt;/code&gt; keyword to your variables so they show in the editor and you can manipulate their value without having to change in the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@export&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_run_speed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nd"&gt;@export&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_acceleration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nd"&gt;@export&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_friction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kyYrcizm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6arx07d91b5ik25ixpc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kyYrcizm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6arx07d91b5ik25ixpc.png" alt="Image description" width="588" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Time to make the player move. I've commented the code so it's easy to follow up on what's happening.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# delta = time between frames
# This function will move the player according to the player's input.
# @param delta: float
# @return void
&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Set the initial vector for the player to be 0
&lt;/span&gt;    &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ZERO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;# Get input vector from player input.
&lt;/span&gt;    &lt;span class="c1"&gt;# Input.get_action_strength("move_right") - Input.get_action_strength("move_left") will return 0 if neither action is pressed, 
&lt;/span&gt;    &lt;span class="c1"&gt;# 1 if move_right is pressed, -1 if move_left is pressed, and a value between -1 and 1 if both actions are pressed.
&lt;/span&gt;    &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_action_strength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;move_right&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_action_strength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;move_left&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;# Input.get_action_strength("move_down") - Input.get_action_strength("move_up") will return 0 if neither action is pressed,
&lt;/span&gt;    &lt;span class="c1"&gt;# 1 if move_down is pressed, -1 if move_up is pressed, and a value between -1 and 1 if both actions are pressed.
&lt;/span&gt;    &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_action_strength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;move_down&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_action_strength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;move_up&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;# Normalize input vector to prevent diagonal movement from being faster.
&lt;/span&gt;    &lt;span class="n"&gt;input_vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;# If input_vector is zero, then the player is not pressing any movement keys.
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;input_vector&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ZERO&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# If the player is not pressing any movement keys, then the player should slow down.
&lt;/span&gt;        &lt;span class="c1"&gt;# move_toward() will move the player toward the target velocity, but will not exceed the target velocity.
&lt;/span&gt;        &lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move_toward&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ZERO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_friction&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;# move_and_slide() will move the player according to the velocity, and will also apply gravity.
&lt;/span&gt;        &lt;span class="nf"&gt;move_and_slide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;# If the player is pressing movement keys, then the player should speed up.
&lt;/span&gt;    &lt;span class="c1"&gt;# move_toward() will move the player toward the target velocity, but will not exceed the target velocity.
&lt;/span&gt;    &lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;move_toward&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_vector&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;_run_speed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_acceleration&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;# move_and_slide() will move the player according to the velocity, and will also apply gravity.
&lt;/span&gt;    &lt;span class="c1"&gt;# move_and_slide() will also stop the player from moving if the player is colliding with a wall.
&lt;/span&gt;    &lt;span class="nf"&gt;move_and_slide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CONGRATULATIONS!
&lt;/h2&gt;

&lt;p&gt;Now you have a player walking around a scene! This is the first step (pun intended) for a working game!&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you!
&lt;/h2&gt;

&lt;p&gt;I hope you find this article helpful!&lt;/p&gt;

&lt;p&gt;See you in the next article!&lt;/p&gt;

</description>
      <category>godot</category>
      <category>gamedev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Creating your first scene</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Fri, 29 Dec 2023 22:54:29 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/creating-your-first-scene-5d5e</link>
      <guid>https://dev.to/eduardojuliao/creating-your-first-scene-5d5e</guid>
      <description>&lt;p&gt;In this article, I'm going to talk about how to create a scene in Godot! For this, we're going to go through some steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Godot version
&lt;/h2&gt;

&lt;p&gt;I'm using Godot 4.1.1 for this series, if I update it, I'll write an article on how to do it, differences and what changes from what I previously wrote 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  Source code
&lt;/h2&gt;

&lt;p&gt;The source code for this article (and series) can be found in my Github under the name &lt;a href="https://github.com/EduardoJuliao/GodotDevTo"&gt;GodotDevTo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a project
&lt;/h2&gt;

&lt;p&gt;Launch Godot, on the right side you're going to find a panel with different options, for now, lets press &lt;strong&gt;+ New Project&lt;/strong&gt;, this will open another window for you to choose the project name and path.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tx4uNVf5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrkgafvahkbmko3jql9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tx4uNVf5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrkgafvahkbmko3jql9u.png" alt="Creating a new project" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I', using Forward+ renderer since it has more functionalities and we're not targeting older systems. &lt;br&gt;
Once all is done, press create and we're good to go!&lt;/p&gt;

&lt;p&gt;What are the differences? &lt;/p&gt;

&lt;h3&gt;
  
  
  Forward+
&lt;/h3&gt;

&lt;p&gt;Forward+ is a modern rendering method, but requires a powerful device. &lt;/p&gt;

&lt;h3&gt;
  
  
  Mobile
&lt;/h3&gt;

&lt;p&gt;Mobile is a simplified rendering method that works well on low-end devices, but has limited features. &lt;/p&gt;

&lt;h3&gt;
  
  
  Compatibility
&lt;/h3&gt;

&lt;p&gt;Compatibility is a legacy rendering method that is compatible with older devices and drivers, but has poor performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Learn more on rendering
&lt;/h3&gt;

&lt;p&gt;Godot documentation has a &lt;a href="https://docs.godotengine.org/en/stable/contributing/development/core_and_modules/internal_rendering_architecture.html"&gt;extensive article&lt;/a&gt; on how the different types of rendering works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Welcome to Godot
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--34lSUr9o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xn5wz5f37a852by6rqya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--34lSUr9o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xn5wz5f37a852by6rqya.png" alt="Godot land page" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have a project created, let's get started by getting used to what we're seeing 😄&lt;/p&gt;

&lt;h3&gt;
  
  
  Scene
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kyPoawnu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qae4r6efcvkgjd1efnwl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kyPoawnu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qae4r6efcvkgjd1efnwl.png" alt="Image description" width="286" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this area, you have everything related to your scene, all your nodes such as player, camera, UI etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scene editor
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Jr_Fv_tB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ch9pu7nqwoorbct1tbh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Jr_Fv_tB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ch9pu7nqwoorbct1tbh.png" alt="Image description" width="800" height="511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is where you're going to create your scene, by moving nodes around and creating your world/&lt;/p&gt;

&lt;h3&gt;
  
  
  Upper panel
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wI2MXhXa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux7aymqlj7c3mttscrvh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wI2MXhXa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ux7aymqlj7c3mttscrvh.png" alt="Image description" width="351" height="76"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In here you can change the scene editor to be 2D, 3D, script and open Godot asset lib.&lt;/p&gt;

&lt;p&gt;Let's go ahead and select 2D since we're creating a 2D Game.&lt;/p&gt;

&lt;h4&gt;
  
  
  Note on Script
&lt;/h4&gt;

&lt;p&gt;Since we're using VS Code as our editor, opening a script will open VS Code and will not use the script editor the engine provides.&lt;/p&gt;

&lt;h3&gt;
  
  
  File System
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z2uiEKRy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lv0vhhdxkqis11odvwmu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z2uiEKRy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lv0vhhdxkqis11odvwmu.png" alt="Image description" width="283" height="677"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This area shows all files related to the project, assets, sprites, scripts, nodes, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bottom Panel
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aONAltwy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkaiarsj5saawfexrpb9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aONAltwy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkaiarsj5saawfexrpb9.png" alt="Image description" width="590" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here you can find some cool windows to help you with yout project! &lt;/p&gt;

&lt;h3&gt;
  
  
  Inspector
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LfIAAaNB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3eb8b9s6ahx0u6nhqx4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LfIAAaNB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3eb8b9s6ahx0u6nhqx4.png" alt="Image description" width="283" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inspector is divided into 3 tabs:&lt;/p&gt;

&lt;h4&gt;
  
  
  Inspector
&lt;/h4&gt;

&lt;p&gt;Shows the properties of the selected node.&lt;/p&gt;

&lt;h4&gt;
  
  
  Node
&lt;/h4&gt;

&lt;p&gt;Shows details to the selected node, you can find Signals and Groups here (We're going to tackle these later)&lt;/p&gt;

&lt;h4&gt;
  
  
  History
&lt;/h4&gt;

&lt;p&gt;The edits the selected node suffered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Scene
&lt;/h2&gt;

&lt;p&gt;FINALLY! What we're being waiting for, how to create a scene!&lt;/p&gt;

&lt;p&gt;And it's pretty easy, in the Scene panel, click on &lt;strong&gt;2D Scene&lt;/strong&gt; option and that's it, you have a scene! &lt;/p&gt;

&lt;p&gt;Don't believe me? Let's create out "player".&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the Player
&lt;/h2&gt;

&lt;p&gt;To create a player, right click on &lt;strong&gt;Node2D&lt;/strong&gt; option and select &lt;strong&gt;+ Add child node&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z_Wq-u4---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbj4r1dg6zdivfptnlg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z_Wq-u4---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbj4r1dg6zdivfptnlg0.png" alt="Image description" width="473" height="177"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A new window will open, in there, search for &lt;strong&gt;CharacterBody2D&lt;/strong&gt;, select it and press create.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r6QpUPwG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezbyiyzg50nrlgzrk6cn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r6QpUPwG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezbyiyzg50nrlgzrk6cn.png" alt="Image description" width="800" height="654"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have a player node in our scene 😁... granted it does nothing, for now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eMWr-RBr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yt70z6u1ffcscbn2o6c5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eMWr-RBr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yt70z6u1ffcscbn2o6c5.png" alt="Image description" width="287" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note there's a warning message on the right side of it, it's telling us we need a collision node attached to it. Don't worry about it for now, as I'm going to cover it in the next articles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add a sprite to our player
&lt;/h3&gt;

&lt;p&gt;Same as we did to our scene, right click the &lt;strong&gt;CharacterBody2D&lt;/strong&gt; and &lt;strong&gt;+ Add Child Node&lt;/strong&gt;. This time, look for Sprite2D, select it and press create.&lt;br&gt;
For now it's just for testing as we don't have a propper sprite for our player.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IILRbvPp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wugy2nfjs8njfftmhi8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IILRbvPp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wugy2nfjs8njfftmhi8.png" alt="Image description" width="800" height="654"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding a sprite to our Sprite 2D
&lt;/h3&gt;

&lt;p&gt;Let's use the &lt;code&gt;icon.svg&lt;/code&gt; that's already in our File System. Select the &lt;code&gt;Sprite2D&lt;/code&gt; Node and in inspector, drag and drop the &lt;code&gt;icon.svg&lt;/code&gt; into the Texture Property.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dYPxVY0B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ahgo2pbz39emkjoa4iw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dYPxVY0B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ahgo2pbz39emkjoa4iw.png" alt="Image description" width="582" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And let's move our &lt;code&gt;CharacterBody2D&lt;/code&gt; to the middle of the screen, so we can easily find it.&lt;br&gt;
Here are the coordinates I used.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--egCJY92l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo47s3ynfj4nigvtv328.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--egCJY92l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eo47s3ynfj4nigvtv328.png" alt="Image description" width="575" height="970"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the scene
&lt;/h2&gt;

&lt;p&gt;To run our scene, we need to first save it. So let's create a Scenes folder in our &lt;code&gt;File System&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Right click the &lt;code&gt;res://&lt;/code&gt; folder &amp;gt; &lt;strong&gt;Create new &amp;gt; Folder&lt;/strong&gt; and name it Scenes. Now, select the base Node and save it in the Scenes Folder, I named this scene &lt;code&gt;FirstScene&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that everything is setup, press F6 (this will run the selected scene).&lt;/p&gt;

&lt;p&gt;This will open a new window with your scene! YAY!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KF3uMYQ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ipnsjwja0c42dknjnc1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KF3uMYQ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ipnsjwja0c42dknjnc1.png" alt="Image description" width="800" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you!
&lt;/h2&gt;

&lt;p&gt;I hope you find this article helpful!&lt;/p&gt;

&lt;p&gt;See you in the next article!&lt;/p&gt;

</description>
      <category>godot</category>
      <category>gamedev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Setting up environment - Godot with VSCode</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Fri, 29 Dec 2023 12:13:39 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/setting-up-environment-godot-with-vscode-1i90</link>
      <guid>https://dev.to/eduardojuliao/setting-up-environment-godot-with-vscode-1i90</guid>
      <description>&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this tutorial, I'm going to explore on how to set Godot (version 4.1, which can be found &lt;a href="https://godotengine.org/download/archive/" rel="noopener noreferrer"&gt;here&lt;/a&gt;) with &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt; on Windows!&lt;/p&gt;

&lt;h2&gt;
  
  
  Requisites
&lt;/h2&gt;

&lt;p&gt;We're going to need to Godot and VS Code installed, alongside some extensions, which I'm going to list them when we're setting up VS Code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Godot
&lt;/h2&gt;

&lt;p&gt;But first, let's change some things in Godot! &lt;br&gt;
In the top bar, select &lt;strong&gt;Editor &amp;gt; Editor Settings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3s68hadjo4w4omizqb10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3s68hadjo4w4omizqb10.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the new window is open, navigate to &lt;strong&gt;Text Editor &amp;gt; External&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhz60x28dm7yazj3v54hu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhz60x28dm7yazj3v54hu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we're going to set up some properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use External Editor&lt;br&gt;
This property must be checked, marking it to be true. This will tell Godot that you want to use other editor to edit your scripts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exec Path&lt;br&gt;
This is the path to the external editor &lt;code&gt;.exe&lt;/code&gt; file, in our case, VS Code. Grab the full path with the &lt;code&gt;.exe&lt;/code&gt; file name and paste it here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exec Flags&lt;br&gt;
Last but not least, we need to use some flags to start VS Code properly: &lt;code&gt;{project} --goto {file}:{line}:{col}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we need to make sure the &lt;strong&gt;Language Server&lt;/strong&gt; is correct, in the same window, navigate to &lt;strong&gt;Network &amp;gt; Language Server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6im2u5tmq0tjssmipbwo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6im2u5tmq0tjssmipbwo.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Remote Server&lt;br&gt;
You can leave as 127.0.0.1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remote Port&lt;br&gt;
Make sure it's set to 6005, as the VS Code will use this port to communicate. It can be changed to another port, but you'll need to update in VS Code settings so they can match.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable Smart Resolve / Show Native Symbols in Editor&lt;br&gt;
Set these two properties to true as will help with IntelliSense.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Thread&lt;br&gt;
¯\&lt;em&gt;(ツ)&lt;/em&gt;/¯&lt;br&gt;
I leave it off as it makes no difference.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ok, everything is set up in Godot, now we need to make some changes in VS Code!&lt;/p&gt;
&lt;h2&gt;
  
  
  VS Code
&lt;/h2&gt;

&lt;p&gt;Let's start by installing some Extensions.&lt;/p&gt;
&lt;h3&gt;
  
  
  Extensions
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfucfdb6lbjhxru3uovr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfucfdb6lbjhxru3uovr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/godotengine/godot-csharp-vscode" rel="noopener noreferrer"&gt;C# Tools for Godot&lt;/a&gt;
This extensions helps when using C# with Godot, and with debugging!&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/AlfishSoftware/godot-files-vscode" rel="noopener noreferrer"&gt;Godot files&lt;/a&gt;
Helps with file paths resolve.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/godotengine/godot-vscode-plugin" rel="noopener noreferrer"&gt;Godot tools&lt;/a&gt;
The main Extension we need, this extension will connect with Godot Engine through the ports that we stetted previously and make the editor work 😄&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Settings
&lt;/h3&gt;

&lt;p&gt;Once the extensions are installed, we need to change some settings.&lt;/p&gt;

&lt;p&gt;Open VS Code settings and search for &lt;code&gt;Gdscript_lsp_server_port&lt;/code&gt;, make sure it's set up to 6005 (the same port we setup before in Godot).&lt;/p&gt;
&lt;h3&gt;
  
  
  Debugging
&lt;/h3&gt;

&lt;p&gt;To create a new &lt;code&gt;launch.json&lt;/code&gt; file, open &lt;code&gt;Run and Debug&lt;/code&gt; on the left panel of VS Code &amp;gt; Create a launch.json file &amp;gt; GDScript Godot Debug.&lt;/p&gt;

&lt;p&gt;Here's the settings I'm using for the &lt;code&gt;launch.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"configurations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Godot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"godot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"request"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"project"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"port"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;6006&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"debugServer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;6006&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"127.0.0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"launch_game_instance"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"launch_scene"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;And you're all setup! If everything was done correctly, pressing &lt;strong&gt;F5&lt;/strong&gt; in VS Code will start Godot and you're ready to run your game!&lt;/p&gt;

&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Godot needs to be open for the language server to run, without it, there will be no IntelliSense.&lt;/li&gt;
&lt;li&gt;This tutorial was tested using &lt;code&gt;GDScript&lt;/code&gt; language, I'm not sure if this works with C# or C++.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you need more help or anything in this guide is difficult to understand or wrong, let me know!&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you!
&lt;/h2&gt;

&lt;p&gt;Hope you find this article helpful!&lt;/p&gt;

&lt;p&gt;See you in the next one!&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>godot</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Loop audio in Godot</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Mon, 18 Sep 2023 03:31:25 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/loop-audio-godot-5h42</link>
      <guid>https://dev.to/eduardojuliao/loop-audio-godot-5h42</guid>
      <description>&lt;p&gt;In this article, I'm going to show you one way to loop music in Godot! &lt;/p&gt;

&lt;p&gt;But first, we need some software installed before we start.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I'm using version 4.1.1 of the engine.&lt;/li&gt;
&lt;li&gt;To work with the audio, I'm using Audacity.&lt;/li&gt;
&lt;li&gt;Some audio to work on: I'm using the following:  &lt;a href="https://opengameart.org/content/woodcocky" rel="noopener noreferrer"&gt;https://opengameart.org/content/woodcocky&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Working on the audio with Audacity
&lt;/h2&gt;

&lt;p&gt;Open Audacity and import the audio file, you should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuufyic9niipq6doifb5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuufyic9niipq6doifb5f.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the bottom panel, click on the cog wheel and select &lt;code&gt;Start and End of the Selection&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbbywf9z88lngkf7t9x2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbbywf9z88lngkf7t9x2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And on the little drop down to the right, select samples (Godot works with samples):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxy2kairqz19odfofr2bz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxy2kairqz19odfofr2bz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have the information we need! Now, in Audacity, go to File &amp;gt; Export &amp;gt; Export as WAV:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgf4tm7s2znoifw2yekwa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgf4tm7s2znoifw2yekwa.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a scene
&lt;/h2&gt;

&lt;p&gt;Let's start by creating a new &lt;code&gt;2D Scene&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wfibm69jwkos2dm8bag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wfibm69jwkos2dm8bag.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding audio to Godot
&lt;/h2&gt;

&lt;p&gt;Grab the &lt;code&gt;.wav&lt;/code&gt; file we just exported and add to the &lt;code&gt;res://&lt;/code&gt; folder of your project: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs22ckm8hz31mr4zsvsi0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs22ckm8hz31mr4zsvsi0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, with your &lt;code&gt;.wav&lt;/code&gt; file selected, click in the &lt;code&gt;Import&lt;/code&gt; tab in the panel above your &lt;code&gt;res://&lt;/code&gt; folder, let's change some of the properties.&lt;br&gt;
Set &lt;code&gt;Loop Mode&lt;/code&gt; to &lt;code&gt;Foward&lt;/code&gt; and &lt;code&gt;Loop Begin&lt;/code&gt; and &lt;code&gt;Loop End&lt;/code&gt; to the sample values found in Audacity. Then, click &lt;code&gt;Reimport(*)&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4j9xonlurhml5br69my4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4j9xonlurhml5br69my4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Audio Stream to the scene
&lt;/h2&gt;

&lt;p&gt;In the scene tab, right click &lt;code&gt;Node2D&lt;/code&gt; and add a new child node of &lt;br&gt;
 the type&lt;code&gt;AudioStreamPlayer2D&lt;/code&gt;: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foq4hp6ai5a1tkyelg6qe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foq4hp6ai5a1tkyelg6qe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5gm82poovex9d7atvxul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5gm82poovex9d7atvxul.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the newly created &lt;code&gt;AudioStreamPlayer2D&lt;/code&gt; under &lt;code&gt;Node2D&lt;/code&gt; and this will open an Inspector for this node. In the &lt;code&gt;Stream&lt;/code&gt; property of your audio player, drag and drop the &lt;code&gt;.wav&lt;/code&gt; file, make sure &lt;code&gt;Autoplay&lt;/code&gt; is enabled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0bvk55dx4zio7hvpl90.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0bvk55dx4zio7hvpl90.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing
&lt;/h2&gt;

&lt;p&gt;Save your scene and press &lt;code&gt;F6&lt;/code&gt; to run the current scene (or press this button).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fizy0kk9mny4pipsuj3by.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fizy0kk9mny4pipsuj3by.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, your audio will play normally, but will loop between the samples you've set int the &lt;code&gt;Loop Begin&lt;/code&gt; and &lt;code&gt;End&lt;/code&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you
&lt;/h2&gt;

&lt;p&gt;Switching engines is always a challenge, but learning Godot for the past few days feels great, the engine is lightweight, and &lt;code&gt;GDScript&lt;/code&gt; is a delight to work with.&lt;/p&gt;

&lt;p&gt;Hope you find this article helpful!&lt;/p&gt;

&lt;p&gt;See you in the next one!&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>godot</category>
      <category>beginners</category>
      <category>audio</category>
    </item>
    <item>
      <title>Adding Items - Coin</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Sat, 12 Aug 2023 14:59:15 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/adding-items-coin-1ejh</link>
      <guid>https://dev.to/eduardojuliao/adding-items-coin-1ejh</guid>
      <description>&lt;h2&gt;
  
  
  Creating a coin
&lt;/h2&gt;

&lt;p&gt;As we're just prototyping our game, we're going to create a simple circle 2D sprite, you can do that by right-clicking the project area, then Create &amp;gt; 2D &amp;gt; Sprites &amp;gt; Circle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fatyplpzwvjujerzo5bg7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fatyplpzwvjujerzo5bg7.png" alt="Creating a simple 2d circle sprite"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the sprite is created, Create a new Empty Object in the Hierarchy, rename it to Coin, and drag and drop the sprite into this new game object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flojpg9xyjpz0zrbp2vfe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flojpg9xyjpz0zrbp2vfe.png" alt="Adding circle collider 2D to the Coin Object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, lets add a Circle collider to this game object.&lt;/p&gt;

&lt;p&gt;Note that I've marked &lt;code&gt;Is Trigger&lt;/code&gt; to true, and why do we need to set it to true?&lt;br&gt;
This will tell the physics engine to not actually collide with the object, but to be a trigger to an event, this way, the player (or any other object) can pass through.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fok77713xwx5kp9as7gwo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fok77713xwx5kp9as7gwo.png" alt="Add Coin Controller to Con Object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Coin Controller
&lt;/h2&gt;

&lt;p&gt;Now that we have the basics of our coin in place, let's create a &lt;code&gt;CoinController&lt;/code&gt; to our &lt;code&gt;Coin game object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First let's create a new Folder under our scripts folder and call it &lt;code&gt;Objects&lt;/code&gt;, from them, right click it and create a new C# Script, call it &lt;code&gt;CoinController&lt;/code&gt; and add it to our &lt;code&gt;Coin game object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2qnmg2gwvvifu6vuectu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2qnmg2gwvvifu6vuectu.png" alt="Rotate Controller code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code doesn't do much, just a trigger check. In this scenario, it checks if it collided with the player, if so, it destroys itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmd7xo8smtbp3fzyfyo8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmd7xo8smtbp3fzyfyo8t.png" alt="Coin controller code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tags
&lt;/h2&gt;

&lt;p&gt;If you run the game now, when you move the player around and hit the coin, nothing will happen, that's because we haven't tagged our &lt;code&gt;Player game object&lt;/code&gt; properly! To do that, select the Player game object in the hierarchy view and Tag it as &lt;code&gt;Player&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzp7lar87v9l5qno1734r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzp7lar87v9l5qno1734r.png" alt="Changing the Player object Tag to "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Rotate our coin
&lt;/h2&gt;

&lt;p&gt;That's great! We actually have a collectable in our game! (I mean, it doesn't do anything for now but we'll make it better in the future 😉)&lt;/p&gt;

&lt;p&gt;For now, let's create a script to rotate our coin in place, just to add a new animation to it.&lt;br&gt;
First, create a folder in our scripts folder, let's call it, animations. In there, create a new c# script, I've called &lt;code&gt;RotateController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F44nhsoia8uhmsxnukdd7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F44nhsoia8uhmsxnukdd7.png" alt="Add newly created RotateController script to our coin object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code is pretty simple! in the &lt;code&gt;Update&lt;/code&gt; method, rotate the &lt;code&gt;transform&lt;/code&gt; property of our game object. Note, that this script can be attached to ANY game object, making it reusable.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;transfor.Rotate()&lt;/code&gt; accepts 3 parameters, &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt;, which are the 3 axis from a 3d object.&lt;br&gt;
We're passing 0 to x because we don't want to rotate it on the X axis, we're multiplying our &lt;code&gt;speed&lt;/code&gt; field by &lt;code&gt;Time.deltaTtime&lt;/code&gt; and passing down to the &lt;code&gt;y&lt;/code&gt; parameter, and 0 to &lt;code&gt;z&lt;/code&gt; axis.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnybb7xo43px3i8ap8n38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnybb7xo43px3i8ap8n38.png" alt="Rotate controller code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Time.deltaTime
&lt;/h3&gt;

&lt;p&gt;Time.deltaTime is a property that provides the time in seconds between the current and previous frame in Unity. You can use it to make something happen at a constant rate regardless of the framerate3. For example, you can use &lt;code&gt;Time.deltaTime&lt;/code&gt; to move a &lt;code&gt;Game Object&lt;/code&gt; at a certain speed per second.&lt;/p&gt;

&lt;p&gt;You can read more about it in the official &lt;a href="https://docs.unity3d.com/ScriptReference/Time-deltaTime.html" rel="noopener noreferrer"&gt;Official Unity documentation for Time.deltaTime&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks
&lt;/h2&gt;

&lt;p&gt;That's a wrap on our simple collectable object! We can use this concept of Trigger in many places, not only for objects, but to trigger a cutscene for example!&lt;br&gt;
Hope you're enjoying these series and it's easy to follow. Until next chapter!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>Creating abilities - Ground pound</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Fri, 11 Aug 2023 22:21:12 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/creating-abilities-ground-pound-50ho</link>
      <guid>https://dev.to/eduardojuliao/creating-abilities-ground-pound-50ho</guid>
      <description>&lt;p&gt;Cool! Our character can move left, right and jump! (and fall off the edge of the map 😅).&lt;/p&gt;

&lt;p&gt;But we can add something to our character to make our jump more, spicy, what about we add a Ground pound option?&lt;/p&gt;

&lt;p&gt;It's pretty simple and can open a lot of doors for movement interaction, such as breakable ground and force push enemies once we hit the ground.&lt;/p&gt;

&lt;p&gt;Also, it's not needed to create a new binding action in our &lt;code&gt;PlayerInput&lt;/code&gt; script, we're going to use what we've already created and check for 3 things, if the player is holding down on the controller (or S or down arrow key) pressed jump and it's not grounded.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ground Pound code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  New fields
&lt;/h3&gt;

&lt;p&gt;Let's start by creating something similar to &lt;code&gt;jumpForce&lt;/code&gt;, but instead of making our character go up, we'll make our character go down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5m5igrkbef7v00g78o93.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5m5igrkbef7v00g78o93.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And since we've added a &lt;code&gt;Boolean&lt;/code&gt; field to tell us if the player pressed the jump button while grounded, we're going to use the same principle and create a field to tell us if the player met the conditions to do a &lt;code&gt;ground pound&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febdytip9w7k2sshigqza.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febdytip9w7k2sshigqza.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And a couple more fields to help us, such as &lt;code&gt;verticalMovement&lt;/code&gt; ( which follows the same idea of horizontal movement, but for, well, vertical) and &lt;code&gt;baseGravityScale&lt;/code&gt;, which is the default gravity value our &lt;code&gt;RigidBody2D&lt;/code&gt; starts with (in our scenario, 1).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88fsu72nx7odliz5rx5l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88fsu72nx7odliz5rx5l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, set our &lt;code&gt;baseGravityScale&lt;/code&gt; in the &lt;code&gt;Awake&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhdj3xblv8ei66kh1lw0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhdj3xblv8ei66kh1lw0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating our new fields
&lt;/h3&gt;

&lt;p&gt;Let's starting by updating our &lt;code&gt;verticalMovement&lt;/code&gt; by reading the &lt;code&gt;Y&lt;/code&gt; value from our input.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe4kvf2mmi9w0iz7coo9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe4kvf2mmi9w0iz7coo9n.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With our &lt;code&gt;verticalMovement&lt;/code&gt; updating, we can do our check.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F26lmr0alkpcv4bedzy75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F26lmr0alkpcv4bedzy75.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating &lt;code&gt;FixedUpdate&lt;/code&gt; method
&lt;/h3&gt;

&lt;p&gt;Now, it's time to use these new fields in our &lt;code&gt;FixedUpdate&lt;/code&gt; method, which controls the character.&lt;/p&gt;

&lt;p&gt;First, let's update our &lt;code&gt;velocityX&lt;/code&gt; variable, because we don't want to our player to move while ground pound is active.&lt;br&gt;
To do that, we check if &lt;code&gt;isForceDown&lt;/code&gt; is true, if it's true, we set our X movement to 0, otherwise, we keep the same movement logic as before.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuejzu1f5b1jjzocped0a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuejzu1f5b1jjzocped0a.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And if it's force down, we set our &lt;code&gt;gravityScale&lt;/code&gt; in the &lt;code&gt;RigidBody&lt;/code&gt; to our &lt;code&gt;downforce&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsmudyppubr7b6m29mkpe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsmudyppubr7b6m29mkpe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Some other code changes
&lt;/h3&gt;

&lt;p&gt;Since we're creating more and more fields, I've decided to handle them in a single &lt;code&gt;if&lt;/code&gt; statement instead of handling them individually.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgtr62sc649vy4v4po8x0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgtr62sc649vy4v4po8x0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In here, I'm setting &lt;code&gt;isForceDown&lt;/code&gt; and &lt;code&gt;hasJumpButtonTriggered&lt;/code&gt; back to false, and &lt;code&gt;body.gravityScale&lt;/code&gt; back to its default.&lt;/p&gt;

&lt;p&gt;Now that we have a single &lt;code&gt;if&lt;/code&gt; statement to handle these fields, we can remove &lt;code&gt;hasJumpButtonTriggered = false&lt;/code&gt; from our &lt;code&gt;hasJumpButtonTriggered&lt;/code&gt; check, making the code simply:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6u92cmab4j7tn87ccb4j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6u92cmab4j7tn87ccb4j.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Important changes
&lt;/h2&gt;

&lt;p&gt;In some occasions, we're falling to fast to Unity to detect collision, this will break and make the player fall through the ground, to fix that, change the &lt;code&gt;Collision Detection&lt;/code&gt; from &lt;code&gt;Discrete&lt;/code&gt; to &lt;code&gt;Continuous&lt;/code&gt; in the RigidBody2D options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznj9arm2rg1xtyxgv4rx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznj9arm2rg1xtyxgv4rx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks
&lt;/h2&gt;

&lt;p&gt;And that's pretty much it! There's a lot of room for improvements but, its taking shape, slowly but surely! Hope this series is helpful, fun and easy to read! Until next chapter!&lt;/p&gt;

&lt;h2&gt;
  
  
  Full code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlayerMovement&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;PlayerInput&lt;/span&gt; &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Rigidbody2D&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;horizontalMovement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;verticalMovement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;baseGravityScale&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;hasJumpButtonTriggered&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isForceDown&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;jumpForce&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;downForce&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isGrounded&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Transform&lt;/span&gt; &lt;span class="n"&gt;groundCheck&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;LayerMask&lt;/span&gt; &lt;span class="n"&gt;groundLayer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;playerInput&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PlayerInput&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Rigidbody2D&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;baseGravityScale&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gravityScale&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Start is called before the first frame update&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Update is called once per frame&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;horizontalMovement&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RoundToInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadValue&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;verticalMovement&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RoundToInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadValue&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;isGrounded&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Physics2D&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;OverlapCircle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;groundCheck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.02f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;groundLayer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Jump&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;triggered&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;isGrounded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;hasJumpButtonTriggered&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Jump&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;triggered&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;verticalMovement&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isGrounded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;isForceDown&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;FixedUpdate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;velocityX&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isForceDown&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;horizontalMovement&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;velocityX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hasJumpButtonTriggered&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jumpForce&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isForceDown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gravityScale&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;downForce&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isGrounded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;isForceDown&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;hasJumpButtonTriggered&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gravityScale&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;baseGravityScale&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Enable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Disable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>gamedev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Make your character jump in Unity2D</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Tue, 30 Aug 2022 06:24:24 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/make-your-character-jump-in-unity2d-4jkc</link>
      <guid>https://dev.to/eduardojuliao/make-your-character-jump-in-unity2d-4jkc</guid>
      <description>&lt;p&gt;Now that we have our player character moving from left to right, we need to take a step forward and make him jump.&lt;/p&gt;

&lt;h2&gt;
  
  
  Binding Jump action to a button
&lt;/h2&gt;

&lt;p&gt;We need to create a new binding into our &lt;code&gt;PlayerInputController&lt;/code&gt;, this binding will be our Jump binding!&lt;br&gt;
We'll be creating more bindings throughout this tutorial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjtyvwiuezdhttr8qcd94.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjtyvwiuezdhttr8qcd94.png" alt="Create the Jump binding - Gamepad"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we need to create a new binding for the keyboard too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftpp69az1dly9s76jyt4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftpp69az1dly9s76jyt4x.png" alt="Create the Jump binding - Keyboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once created, don't forget to "Save Asset", otherwise the binding will not be created.&lt;/p&gt;

&lt;h2&gt;
  
  
  Jump Code
&lt;/h2&gt;

&lt;p&gt;Same as &lt;code&gt;speed&lt;/code&gt; we will need a new property to tell us the jump force for our character.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmc4mi91j8xhq3zhesd48.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmc4mi91j8xhq3zhesd48.png" alt="jump force property"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On our &lt;code&gt;Update()&lt;/code&gt;, we need to tell that the Y velocity for our character body.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhssleh3np85mdcs77mvx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhssleh3np85mdcs77mvx.png" alt="Initial jump code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Problems
&lt;/h3&gt;

&lt;p&gt;Our character can jump! YAY! But he can jump and jump and jump as long we keep pressing the Jump button and we don't want that.&lt;/p&gt;

&lt;p&gt;To fix that, we need to know if our character is touching the ground before jumping.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is our character Grounded?
&lt;/h2&gt;

&lt;p&gt;Let's add a new game object as a child of our character. This will be our "ground checker".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F06e0vccd9w9qs55q58x3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F06e0vccd9w9qs55q58x3.png" alt="Player character ground checker"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once created, move it slightly down in the Y axis, -0.55 should be enough.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fowpyhboil2e0vbmlswsy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fowpyhboil2e0vbmlswsy.png" alt="Move ground check slightly down"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Couple more things we need to add to our code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Property telling if the player is grounded or not.&lt;/li&gt;
&lt;li&gt;A game object property so the code knows what is our ground check object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LayerMask&lt;/code&gt; so our code knows what is a ground in our game.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's add them!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvvj7x3ez1i7omt13blje.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvvj7x3ez1i7omt13blje.png" alt="ground check properties"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the editor we're going to drag and drop the &lt;code&gt;GroundCheck&lt;/code&gt; game object into our &lt;code&gt;groundCheck&lt;/code&gt; property in the player controller script.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp21t60u5nllg5mrer0ov.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp21t60u5nllg5mrer0ov.png" alt="Ground Check game object attached to the script"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the layer mask, we'll need to do some extra work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Layer Mask
&lt;/h3&gt;

&lt;p&gt;Go into the inspector (can be the player object) and select Layers, and &lt;code&gt;Add Layer...&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkm8w60woi7a785sqfgx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkm8w60woi7a785sqfgx5.png" alt="Add layer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this new screen, create a new layer called &lt;code&gt;Ground&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs66ne5lc7lig7yl0atf4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs66ne5lc7lig7yl0atf4.png" alt="New Ground Layer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have our Layer now, let's change our ground game object layer to be ground.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flex2ncvy72ei7wwgtpm7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flex2ncvy72ei7wwgtpm7.png" alt="Ground game object layer to ground"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And with that, go back to our player input controller, and change the &lt;code&gt;GroundLayer&lt;/code&gt; to &lt;code&gt;Ground&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2n6huudyfu6rfslawoxk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2n6huudyfu6rfslawoxk.png" alt="Ground Layer into the player script controller"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Is Player Character grounded?
&lt;/h2&gt;

&lt;p&gt;We have everything we need in order to make our player jump properly!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnz9ms2if88j3ruqf451f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnz9ms2if88j3ruqf451f.png" alt="Updated jump code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;Physics2D&lt;/code&gt; class from Unity, we'll check if our &lt;code&gt;groundCheck&lt;/code&gt; is overlapping with something, in this case the ground!&lt;/p&gt;

&lt;p&gt;Run the game and keep pressing the jump button, we won't be able to jump more than once!&lt;/p&gt;

&lt;h2&gt;
  
  
  EXTRA!
&lt;/h2&gt;

&lt;p&gt;It's not good to handle physics (like moving the &lt;code&gt;rigidbody&lt;/code&gt; around) in the Update method, so we'll move some of the code to the &lt;code&gt;FixedUpdate()&lt;/code&gt; code, and leave the &lt;code&gt;Update()&lt;/code&gt; method to handle the player input.&lt;/p&gt;

&lt;p&gt;Some extra properties:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbytb8symsbwew3if33zv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbytb8symsbwew3if33zv.png" alt="Extra needed properties"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the actual code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzgq83nupop85awkvsi23.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzgq83nupop85awkvsi23.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks
&lt;/h2&gt;

&lt;p&gt;Thanks for reading! Hope this series is being helpful and fun for you as fun is being writing them! See you in the next chapter!&lt;/p&gt;

&lt;h2&gt;
  
  
  Edit
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Bug fixes
&lt;/h3&gt;

&lt;p&gt;Fixed a bug in the code where you can press space bar multiple times and would trigger a second jump when hitting the ground.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why it happened?
&lt;/h4&gt;

&lt;p&gt;When pressing space bar multiple times, the code would just set &lt;code&gt;hasJumpButtonTriggered&lt;/code&gt; to true in mid air, making a jump action valid and thus, jumping again once hitting the ground.&lt;/p&gt;

&lt;h4&gt;
  
  
  The fix
&lt;/h4&gt;

&lt;p&gt;The fix was pretty easy, moving the &lt;code&gt;isGrounded&lt;/code&gt; check to &lt;code&gt;Update&lt;/code&gt; where the code is checking if it's ground, with this change, &lt;code&gt;hasJumpButtonTriggered&lt;/code&gt; can only be changed to true once the jump key is pressed AND it's grounded&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>gamedev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Moving your character in Unity 2d</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Sat, 20 Aug 2022 22:00:54 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/moving-your-character-in-unity-2d-28do</link>
      <guid>https://dev.to/eduardojuliao/moving-your-character-in-unity-2d-28do</guid>
      <description>&lt;p&gt;Have you ever wanted to create your own game but find it too overwhelming or not knowing where to start?&lt;br&gt;
So let's start with very basic things, like making our character move from A to B!&lt;/p&gt;

&lt;p&gt;In this tutorial we will be using &lt;code&gt;Input System&lt;/code&gt; from &lt;code&gt;Unity&lt;/code&gt; to create a 2D Platformer. It's not available by default so you'll need to add it to your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Input Manager
&lt;/h2&gt;

&lt;p&gt;To do that, simply go select &lt;code&gt;window&lt;/code&gt; in the to bar menu, navigate to &lt;code&gt;Package Manager&lt;/code&gt;, in the package manager window, change the package resource to &lt;code&gt;Unity Registry&lt;/code&gt; and download the &lt;code&gt;Input System&lt;/code&gt; into your project!&lt;/p&gt;

&lt;p&gt;When the package is installed, it'll pop up a warning, saying that it'll change the default input system to the new installed package, hit &lt;code&gt;Yes&lt;/code&gt; and Unity will be restarted.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39cd962wij0mdlxm5stw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39cd962wij0mdlxm5stw.png" alt="Opening up Package Manager"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yak375kusmxqh4jxmlk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yak375kusmxqh4jxmlk.png" alt="Change to unity registry"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0b9v2xlk687nrfe7a79.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff0b9v2xlk687nrfe7a79.png" alt="Install package"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the Input Manager
&lt;/h2&gt;

&lt;p&gt;Now that the Input Manager is installed let's start using it!&lt;/p&gt;

&lt;p&gt;First, let's create a mock for our player character. In the Assets folder, click with the right mouse button and create a new 2D Sprite, I personally use squares for the player mockups, but you can pick whatever sprite you want! With your sprite created, drag it up into the scene so we can have a &lt;code&gt;GameObject&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that you have our &lt;code&gt;Player Game Object&lt;/code&gt; created in our scene, let's add the Input system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftth6aok55ia7g3hv9rkg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftth6aok55ia7g3hv9rkg.png" alt="Attaching the player input"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select &lt;code&gt;Open Input Settings&lt;/code&gt; and in the newly opened window, click &lt;code&gt;Create settings asset&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7mxm05r0g2q8p60lmty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7mxm05r0g2q8p60lmty.png" alt="Create new asset"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faksgys48f698vwrdu8tr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faksgys48f698vwrdu8tr.png" alt="click on Create settings asset"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the settings are created, click in &lt;code&gt;Create actions&lt;/code&gt; in the Player Input component in you Player Game Object, this will prompt you to create a &lt;code&gt;Input Manager&lt;/code&gt; to our player. I created a folder called &lt;code&gt;Input Managers&lt;/code&gt; inside the asset folder and renamed the file name to &lt;code&gt;PlayerInput&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you open this file, you'll see a window with some pre-defined options, like move, which we are going to use in this tutorial!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67iwqmt3ilgoplkmtghc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67iwqmt3ilgoplkmtghc.png" alt="Player Input Actions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't forget to check &lt;code&gt;Generate C# Class&lt;/code&gt; int the recently created &lt;code&gt;PlayerInput&lt;/code&gt;, so we can use in our code!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj3therxos5dp3gzglprz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj3therxos5dp3gzglprz.png" alt="Creating the Player Input Asset"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Player movement
&lt;/h2&gt;

&lt;p&gt;Our setup for inputs is ready! now, we need to make it work with our player!&lt;/p&gt;

&lt;h3&gt;
  
  
  Extra components
&lt;/h3&gt;

&lt;p&gt;There's two components we need to add to our player game object, first, &lt;code&gt;BoxCollider2D&lt;/code&gt;, this component will be responsible to handle collisions and triggers... let's leave it as it is. The second component is &lt;code&gt;Rigidbody2D&lt;/code&gt;, this component will handle our physics, for this component, expand constraints and freeze rotation on the Z axis, this will prevent the game object to rotate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuk1e90dhi8ujnckz9bp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuk1e90dhi8ujnckz9bp.png" alt="Freeze z rotation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Player Controller
&lt;/h3&gt;

&lt;p&gt;To make things more organized, let's create a folder called &lt;code&gt;Scripts&lt;/code&gt; and a folder called &lt;code&gt;PlayerScripts&lt;/code&gt; inside that folder, the structure should look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Assets/
├─ Scripts/
│  ├─ PlayerScripts/
│  │  ├─ PlayerController.cs


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's start defining some properties, let's start with speed! I like to keep these properties &lt;code&gt;private&lt;/code&gt; and decorate them with &lt;code&gt;[SerializeField]&lt;/code&gt;. Decorating it as Serialize Field, you can still access the property in the editor, even if it's private.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And let's get our &lt;code&gt;PlayerInput&lt;/code&gt;.&lt;br&gt;
First we create a property, and we're not going to decorate with &lt;code&gt;[SerializeField]&lt;/code&gt; because we don't want to access it in the editor.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;PlayerInput&lt;/span&gt; &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the Awake method we're going to instantiate our &lt;code&gt;playerInput&lt;/code&gt;.&lt;br&gt;
We need to initialize it on Awake method because the lifecycle of the MonoBehaviour, Awake is called before everything else in the script.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;playerInput&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PlayerInput&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And couple of methods for the player input.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Enable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Disable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We also need to initialize the &lt;code&gt;rigidbody&lt;/code&gt; in our Start method, making it look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;PlayerInput&lt;/span&gt; &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Rigidbody&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;playerInput&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PlayerInput&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Rigidbody&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With all this, we can attach this script to the &lt;code&gt;Player Game Object&lt;/code&gt;, go back to the object and add the component, you can search for the script when adding a component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ior9kdtq3rwahydx7si.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ior9kdtq3rwahydx7si.png" alt="Searching for the player controller"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  GetComponent
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;GetComponent&amp;lt;&amp;gt;()&lt;/code&gt; method is responsible for getting a component attached to the GameObject.&lt;/p&gt;

&lt;h3&gt;
  
  
  Player Movement
&lt;/h3&gt;

&lt;p&gt;For the player movement, we'll handle the in then &lt;code&gt;Update&lt;/code&gt; method, which is called every frame 🤯&lt;/p&gt;

&lt;p&gt;Since the game is a platformer, let's get the X value of the input from the player.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;horizontal&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RoundToInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadValue&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;Mathf.RoundToInt&lt;/code&gt; will make sure the value is -1 when the player is moving left, 0 when no input is detected and 1 when the player ,is moving to the right.&lt;/p&gt;

&lt;p&gt;With the horizontal input known, we can calculate the velocity which we'll move the player game object.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;velocityX&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;horizontal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And finally, MOVE THE PLAYER! By changing the body velocity.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;velocityX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But there's a thing, if we start the game our &lt;code&gt;Player Game Object&lt;/code&gt; will start falling into the void...&lt;/p&gt;

&lt;p&gt;To fix this problem, let's create a floor for our player to be on.&lt;br&gt;
Same as creating a player, create a new Square Sprite (make things a little bit easier), drag and drop into the scene and expand it on the X axis.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmc2600stbo14bbfu7heg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmc2600stbo14bbfu7heg.png" alt="Floor object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yxqjwstgxe88hrzse3j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yxqjwstgxe88hrzse3j.png" alt="Floor object scale"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And add &lt;code&gt;BoxCollider2D&lt;/code&gt; and &lt;code&gt;RigidBody2D&lt;/code&gt; to the floor game object and mark the &lt;code&gt;Body Type&lt;/code&gt; in the rigidbody component as &lt;code&gt;Static&lt;/code&gt;, so it won't move.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting the scene
&lt;/h2&gt;

&lt;p&gt;If you reached here you should have a moving player object!&lt;br&gt;
The best part of it, you can move the player using &lt;code&gt;A&lt;/code&gt;, &lt;code&gt;D&lt;/code&gt;, &lt;code&gt;Left arrow key&lt;/code&gt; and &lt;code&gt;Right arrow key&lt;/code&gt; on the keyboard and if you have a controller connected, you can move it by using the left joystick!&lt;/p&gt;

&lt;p&gt;In the next post we're making the player jump!&lt;/p&gt;

&lt;p&gt;Update:&lt;/p&gt;

&lt;p&gt;Full Player Script&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlayerMovement&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;PlayerInput&lt;/span&gt; &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Rigidbody2D&lt;/span&gt; &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;playerInput&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PlayerInput&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;rb&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Rigidbody2D&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Start is called before the first frame update&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Update is called once per frame&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;horizontal&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RoundToInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadValue&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;().&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;velocityX&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;horizontal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;velocityX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Enable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;playerInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Disable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>gamedev</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Event Stream - Server Side</title>
      <dc:creator>Eduardo Julião</dc:creator>
      <pubDate>Thu, 22 Jul 2021 20:38:23 +0000</pubDate>
      <link>https://dev.to/eduardojuliao/event-stream-server-side-55n2</link>
      <guid>https://dev.to/eduardojuliao/event-stream-server-side-55n2</guid>
      <description>&lt;p&gt;For this post, we're going to explore a way to stream data to the client using a Web Api and C#.&lt;/p&gt;

&lt;p&gt;Picture this requirement, you need to process an array of data, lets say of a 100 items, once each item is processed, the api should return data to the client, without SignalR.&lt;/p&gt;

&lt;p&gt;To do this, in this example, we will have a worker that will return a &lt;code&gt;IAsyncEnumerable&lt;/code&gt; and between each iteration, the method will wait between 5 to 10 seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Service.cs&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;Random&lt;/span&gt; &lt;span class="n"&gt;_random&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Random&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;IAsyncEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Enumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Wait a random amount of time to simulate some work.&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;secondsToWait&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Timespan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondsToWait&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the simulated service in place, we can move on to the &lt;code&gt;Controller&lt;/code&gt;. For the endpoint, we're going to have an &lt;code&gt;async Task&lt;/code&gt; &lt;code&gt;Get&lt;/code&gt; method that will write the result to the Response and send it for every item it was processed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// StreamController.cs&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IService&lt;/span&gt; &lt;span class="n"&gt;_service&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;StreamController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IService&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_service&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;HttpGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{amount:int:min(1)}"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;GetStreamData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Set Content-Type to text/event-stream&lt;/span&gt;
    &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"text/event-stream"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;_service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// this can be anything, even just a json object&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;dataItem&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$"data: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;\n\n"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Convert the text to a byte array&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;utf8DataitemBytes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataItem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Write the byte array to the HttpResponse&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;utf8DataitemBytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;utf8DataitemBytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;// Push&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FlushAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the endpoint is called, it'll send a new "line" of data instead of waiting for all items to be processed to be sent at once.&lt;/p&gt;

</description>
      <category>csharp</category>
    </item>
  </channel>
</rss>
