<?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: Genoma Games</title>
    <description>The latest articles on DEV Community by Genoma Games (@genomagames).</description>
    <link>https://dev.to/genomagames</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%2Forganization%2Fprofile_image%2F2802%2F740b2417-4c53-4795-9349-5fca020ff066.png</url>
      <title>DEV Community: Genoma Games</title>
      <link>https://dev.to/genomagames</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/genomagames"/>
    <language>en</language>
    <item>
      <title>Using Unity UI for in-game HUDs</title>
      <dc:creator>Alberto Fernandez Medina</dc:creator>
      <pubDate>Wed, 14 Oct 2020 22:00:00 +0000</pubDate>
      <link>https://dev.to/genomagames/genoma-invaders-using-unity-ui-for-in-game-huds-pjc</link>
      <guid>https://dev.to/genomagames/genoma-invaders-using-unity-ui-for-in-game-huds-pjc</guid>
      <description>&lt;p&gt;This article covers how to implement a basic &lt;strong&gt;U&lt;/strong&gt; ser &lt;strong&gt;I&lt;/strong&gt; nterface on Unity using Unity UI to display info to players.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of Unity&lt;/li&gt;
&lt;li&gt;Complete &lt;a href="https://genomagames.com/genoma-invaders/03-unity-physics-2d-using-collider2d-and-kinematic-rigidbody2d/" rel="noopener noreferrer"&gt;“ &lt;strong&gt;Unity Physics 2D Using Collider2D and Kinematic Rigidbody2D&lt;/strong&gt; ”&lt;/a&gt; or checkout the code from &lt;a href="https://github.com/GenomaGames/genoma-invaders/tree/tutorial/03" rel="noopener noreferrer"&gt;Genoma Invaders’ Github repository, branch &lt;code&gt;tutorial/03&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Last time we end up updating all the movement logic to be handled by Unity’s Physics engine and make use of its collision system to &lt;strong&gt;detect our player’s bullets colliding with the enemies to be destroyed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This time we’ll be showing the Score Points on the game screen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unity’s UI
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/UIToolkits.html" rel="noopener noreferrer"&gt;Unity has three types of “UI Toolkits”&lt;/a&gt; to display user interfaces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;UIElements&lt;/strong&gt; : This is a new UI Toolkit in active development.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unity UI&lt;/strong&gt; : This is the most used UI Toolkit for Unity until now.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IMGUI&lt;/strong&gt; : This is a UI Toolkit commonly used to display development UIs due to its simplicity and ease to use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will be using &lt;a href="https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/index.html" rel="noopener noreferrer"&gt;&lt;strong&gt;Unity UI&lt;/strong&gt;&lt;/a&gt; for our game UI, starting with the score display.&lt;/p&gt;

&lt;p&gt;To display our player’s score points, we will need to use the &lt;a href="https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/UIVisualComponents.html#text" rel="noopener noreferrer"&gt;&lt;strong&gt;Text Visual Component&lt;/strong&gt;&lt;/a&gt;. Let’s start by adding one to our Game Scene.&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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F01-Unity_Hierarchy_Create_UI_Text.gif" 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F01-Unity_Hierarchy_Create_UI_Text.gif" alt="01-Unity_Hierarchy_Create_UI_Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Canvas and Layout
&lt;/h3&gt;

&lt;p&gt;As you can see, this will create 3 Game Objects in our Hierarchy Window, a Canvas containing Text, and something called EventSystem.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/UICanvas.html" rel="noopener noreferrer"&gt;Canvas&lt;/a&gt; serves to define an area that will render UI components. It has different render options to help place UI Components all over our game world (checkout the docs!). The &lt;strong&gt;Render Mode&lt;/strong&gt; we will be using is the “Screen Space - Overlay” set by default.&lt;/li&gt;
&lt;li&gt;The Text Object is the one in charge of display our text.&lt;/li&gt;
&lt;li&gt;The EventSystem is a subsystem in charge of controlling the UI events and inputs. It handles navigation, input activation, and selection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Select the Text Object and rename it to &lt;code&gt;Score Text&lt;/code&gt;, then go to its Text Component and put some placeholder score in its Text input (like &lt;code&gt;1978&lt;/code&gt;) to have a reference of how it would look.&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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F02-Unity_Inspector_Text.jpg" 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F02-Unity_Inspector_Text.jpg" alt="02-Unity_Inspector_Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you may probably have noticed that there is no text shown in the Scene Window. This happens because the Canvas, as we said, is like a sticker in our camera, it is not part of the game world. To center our Game Window in the Canvas Game Object, select it in the Hierarchy Window and then, with the cursor over the Game Window, press F, this will move the Game Window view to fit the Game Object you have selected, in this case, the Canvas.&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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F03-Unity_Game_Object_Focus.gif" 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F03-Unity_Game_Object_Focus.gif" alt="03-Unity_Game_Object_Focus"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the canvas on sigh now, it’s time to position the Score Text, select it, and in its Inspector Window under the Rect Transform section, click on the icon similar to a red reticle, this is the Anchor Presets selector. Now with the Anchor Presets selector opened when pressing Shift+Alt&amp;lt;/kbd&amp;gt; you'll see that the selection icon change, select the one under the row with "top" label and the column with "right" label while pressing Shift+Alt&amp;lt;/kbd&amp;gt;. This will place the Score Text Object on the top right corner of the Canvas and anchor 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F04-Unity_UI_Object_Anchor_Preset.gif" 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F04-Unity_UI_Object_Anchor_Preset.gif" alt="04-Unity_UI_Object_Anchor_Preset"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can read more about Rect Transform Component in &lt;a href="https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/UIBasicLayout.html" rel="noopener noreferrer"&gt;Unity UI Documentation - Basic Layout section&lt;/a&gt;&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;If you now switch to Game Window 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F05-Unity_Game_With_Score_UI.jpg" 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F05-Unity_Game_With_Score_UI.jpg" alt="05-Unity_Game_With_Score_UI"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Unity UI Text Style
&lt;/h3&gt;

&lt;p&gt;Now that our Text is positioned in the Canvas, let’s give it some styles and appearance similar to our game visuals.&lt;/p&gt;

&lt;p&gt;First, we are going to change the font used for the text. We’ll be using &lt;strong&gt;Press Start 2P&lt;/strong&gt; you can download it from &lt;a href="http://www.zone38.net/font/#pressstart" rel="noopener noreferrer"&gt;codeman38’s webpage&lt;/a&gt; (the author). We placed the fonts under &lt;code&gt;/Assets/Genoma Invaders/Fonts/Press Start P2/&lt;/code&gt; to organize the project’s files.&lt;/p&gt;

&lt;p&gt;To change the font for the Score Text Object:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select it and change the Font Property under its Text Component.&lt;/li&gt;
&lt;li&gt;Set its Font Size Property to &lt;code&gt;8&lt;/code&gt; (this is the recommended size for this font),&lt;/li&gt;
&lt;li&gt;Update the Alignment to the right and centered vertically.&lt;/li&gt;
&lt;li&gt;Update the Color Property to be White (&lt;code&gt;FFFFFF&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F06-Unity_UI_Text_Styles.jpg" 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F06-Unity_UI_Text_Styles.jpg" alt="06-Unity_UI_Text_Styles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a final tweak to adjust the size and position of the element, go to the Score Text Object’s Rect Transform Component and set the next properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pos X: -8&lt;/li&gt;
&lt;li&gt;Pos Y: -8&lt;/li&gt;
&lt;li&gt;Pos Z: 0&lt;/li&gt;
&lt;li&gt;Width: 64&lt;/li&gt;
&lt;li&gt;Height: 8&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; You should see the changes applied in the Game Window (remember to set the Taito 8080 resolution configured in the &lt;a href="https://genomagames.com/genoma-invaders/01-getting-started-with-unity-2d-game-development/#arcade-cabinet-taito-8080" rel="noopener noreferrer"&gt;first lesson&lt;/a&gt;).&lt;/em&gt;&lt;/small&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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F07-Unity_Game_with_Score.jpg" 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F07-Unity_Game_with_Score.jpg" alt="07-Unity_Game_with_Score"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating Unity UI Text
&lt;/h3&gt;

&lt;p&gt;The Score Text Object is ready to display our Game Score, and now we need some logic to update its Text value.&lt;/p&gt;

&lt;p&gt;Create a new C# Script in the Project Window called &lt;code&gt;Score.cs&lt;/code&gt; and place it under &lt;code&gt;/Assets/Genoma Invaders/UI/&lt;/code&gt; (new folder). We will use this Component to maintain the Score points for our Game.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Score.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine.UI&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;class&lt;/span&gt; &lt;span class="nc"&gt;Score&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;score&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="c1"&gt;// https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Text.html&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.Awake.html&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;text&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;Text&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;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.Start.html&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;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;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;();&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;void&lt;/span&gt; &lt;span class="nf"&gt;AddScore&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;points&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&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;The previous code does set the Text value to the initial score (&lt;code&gt;0&lt;/code&gt;) whenever the Game Starts and update it when someone calls the method &lt;code&gt;AddScore&lt;/code&gt;. Attach it to the Score Text Object and Play the Game, you should see the Score Points to &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With this new Component attached to the Text Score Object, we can use the &lt;code&gt;Score.AddScore&lt;/code&gt; method to increment our Game Score. Let’s update the &lt;code&gt;Enemy.cs&lt;/code&gt; to communicate with the Score Component inside the Score Text Object. We need to find and store a reference to the Score Component to call its &lt;code&gt;AddScore&lt;/code&gt; method whenever the player kills an Enemy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Enemy.cs&lt;/code&gt;&lt;/strong&gt;&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;// ...&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Enemy&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="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Score&lt;/span&gt; &lt;span class="n"&gt;score&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="c1"&gt;// ...&lt;/span&gt;

        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Object.FindObjectOfType.html&lt;/span&gt;
        &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FindObjectOfType&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Score&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;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.OnDestroy.html&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;OnDestroy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddScore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Increment game score by 100&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;This way, whenever &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Object.Destroy.html" rel="noopener noreferrer"&gt;&lt;code&gt;Destroy&lt;/code&gt;&lt;/a&gt; method is called (remember that the Bullet Component is calling this method over the Enemy Object) with a Game Object containing an Enemy Component, it will update the Score points before being destroyed from the Scene.&lt;/p&gt;

&lt;p&gt;Let’s try 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F08-Unity_Game_with_Score_Change.gif" 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%2Fgenomagames.com%2Fgenoma-invaders%2Fassets%2Flesson-04%2F08-Unity_Game_with_Score_Change.gif" alt="08-Unity_Game_with_Score_Change"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;We ended up with a game displaying a score text that updates whenever an enemy is killed and learning a bit about Unity UI systems.&lt;/p&gt;

&lt;p&gt;You can get the code generated by following this tutorial in &lt;a href="https://github.com/GenomaGames/genoma-invaders/tree/tutorial/04" rel="noopener noreferrer"&gt;Genoma Invaders’ Github repository, branch &lt;code&gt;tutorial/04&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Game Dev! 👾&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>gamedev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Unity Physics 2D Using Collider2D and Kinematic Rigidbody2D</title>
      <dc:creator>Alberto Fernandez Medina</dc:creator>
      <pubDate>Tue, 25 Aug 2020 22:00:00 +0000</pubDate>
      <link>https://dev.to/genomagames/genoma-invaders-unity-physics-2d-using-collider2d-and-kinematic-rigidbody2d-47l5</link>
      <guid>https://dev.to/genomagames/genoma-invaders-unity-physics-2d-using-collider2d-and-kinematic-rigidbody2d-47l5</guid>
      <description>&lt;p&gt;Learn how to handle 2D game objects collisions while making a space invaders 2D game using the Unity physics 2D engine with 2D colliders and kinematic 2D rigid bodies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://store.unity.com/download?ref=personal"&gt;&lt;strong&gt;Unity 2020.1&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://visualstudio.microsoft.com/vs/"&gt;&lt;strong&gt;Visual Studio Editor&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Complete &lt;a href="https://dev.to/genomagames/getting-started-in-unity-2d-game-development-583k"&gt;“ &lt;strong&gt;How to Move 2D Objects in Unity&lt;/strong&gt; ”&lt;/a&gt; or checkout the code from &lt;a href="https://github.com/GenomaGames/genoma-invaders/tree/tutorial/02"&gt;Genoma Invaders’ Github repository, branch &lt;code&gt;tutorial/02&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;In the last tutorial, we walked through &lt;strong&gt;How to Move 2D Objects in Unity&lt;/strong&gt; using the Unity’s Scripting API, ending up with a Scene containing a Player Game Object that moves when pressing directional input buttons and shooting bullets that move upwards in a consistent manner when a &lt;code&gt;Fire1&lt;/code&gt; related input is pressed.&lt;/p&gt;

&lt;p&gt;Our next steps will be to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make Bullets collide with the Enemies&lt;/li&gt;
&lt;li&gt;Organize our code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Until now, the Game Objects of our game were moving by updating its &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Transform-position.html"&gt;&lt;code&gt;Transform.position&lt;/code&gt;&lt;/a&gt; values by calling &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Transform.Translate.html"&gt;&lt;code&gt;Transform.Translate&lt;/code&gt;&lt;/a&gt; method. But, to let Unity detect collisions between objects, this is a bad practice, we need to change this approach to make use of Unity’s Physics 2D engine so that it can notify us about collisions between Game Objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unity’s Kinematic Rigidbody2D, Moving Objects with Physics
&lt;/h2&gt;

&lt;p&gt;To move a Game Object using Unity’s 2D Physics we need to add &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/class-Rigidbody2D.html"&gt;&lt;code&gt;Rigidbody2D&lt;/code&gt;&lt;/a&gt; Component to it and call &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.MovePosition.html"&gt;&lt;code&gt;Rigidbody2D.MovePosition&lt;/code&gt;&lt;/a&gt; to move it, this way the Physics2D engine can handle the movement and take care of the physics calculations implied on it.&lt;/p&gt;

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

&lt;p&gt;Let’s start with the Player Game Object by adding the Rigidbody2D Component to it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9WXUBYH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/01-Unity_Inspector_Add_Rigidbody2D_to_Player.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9WXUBYH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/01-Unity_Inspector_Add_Rigidbody2D_to_Player.gif" alt="01-Unity_Inspector_Add_Rigidbody2D_to_Player" width="405" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now there is a small point in adding physics to game objects in Unity. If you play the game, you will see how our Player Game Object now falls into the abyss.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x-4rw2LA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/02-Unity-Game_Player_Falling.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x-4rw2LA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/02-Unity-Game_Player_Falling.gif" alt="02-Unity-Game_Player_Falling" width="398" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;“Goodbye dear friend!”&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The Player Game Object is falling because Unity’s 2D Physics engine is now applying gravity to the Player Game Object. The thing is, we don’t want gravity in this game. We want them to move whenever we want them to move. We are only going to use Unity’s 2D Physics engine for collision detection. To achieve this, we need to switch the &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/class-Rigidbody2D.html"&gt;&lt;code&gt;Rigidbody2D.bodyType&lt;/code&gt;&lt;/a&gt; property.&lt;/p&gt;

&lt;p&gt;Using the Unity Inspector window, change the Player’s Rigidbody2D Body Type property to &lt;code&gt;Kinematic&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6VthJYmA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/03-Unity_Inspector_Change_Rigidbody2D_bodyType_to_Kinematic.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6VthJYmA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/03-Unity_Inspector_Change_Rigidbody2D_bodyType_to_Kinematic.gif" alt="03-Unity_Inspector_Change_Rigidbody2D_bodyType_to_Kinematic" width="276" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Now if you play the game will see that the Player stay still.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Kinematic Rigidbodies will ignore all forces and collision applied to them, so it will stay still unless moved by scripting. One thing more to do with the Rigidbody2D properties is to check “ &lt;strong&gt;Use Full Kinematics Contacts&lt;/strong&gt; ” checkbox to tell the Unity’s 2D Physics engine that this object can collide with other kinematic objects.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Checkout &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/class-Rigidbody2D.html"&gt;&lt;strong&gt;Unity’s Manual section “Rigidbody 2D”&lt;/strong&gt;&lt;/a&gt; to have more context about this topic.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RDR51Ojd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/04-Unity_Game_Player_Moving_with_Kinematic_Rigidbody2D.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RDR51Ojd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/04-Unity_Game_Player_Moving_with_Kinematic_Rigidbody2D.gif" alt="04-Unity_Game_Player_Moving_with_Kinematic_Rigidbody2D" width="400" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There, our Player is now in position. It’s time to update Player’s Component code to use &lt;code&gt;RigidBody2D&lt;/code&gt; instead of &lt;code&gt;Transform&lt;/code&gt; to move the Player Game Object.&lt;/p&gt;

&lt;p&gt;First, we need the Player Component to depends on Rigidbody2D Component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/RequireComponent.html&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;RequireComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Rigidbody2D&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;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Some code has been hidden under &lt;code&gt;// ...&lt;/code&gt; to show only relevant code.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;By using the &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/RequireComponent.html"&gt;&lt;code&gt;RequireComponent&lt;/code&gt;&lt;/a&gt; Attribute, whenever a Player Component is added to a Game Object, if there is not Rigidbody2D Component attached, it will be added.&lt;/p&gt;

&lt;p&gt;Now we need the Player Component to store a reference to the RigidBody2D Component to communicate with it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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;// ...&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="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;2&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;GameObject&lt;/span&gt; &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.html&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;rb2D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.Awake.html&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="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Component.GetComponent.html&lt;/span&gt;
        &lt;span class="n"&gt;rb2D&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="k"&gt;private&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="c1"&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;We have added a new method to our Player Component called &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.Awake.html"&gt;&lt;code&gt;Awake&lt;/code&gt;&lt;/a&gt;, and inside we are using &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Component.GetComponent.html"&gt;&lt;code&gt;GetComponent&lt;/code&gt;&lt;/a&gt; method to obtain a reference to the attached Rigidbody2D. &lt;code&gt;Awake&lt;/code&gt; method serves to hook any logic needed for our Component when the game starts a Scene or when a Game Object containing this Component is instantiated. It is usually used to setup references like we did for the Rigidbody2D Component.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; To get more info about this topic checkout &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/ExecutionOrder.html"&gt;Unity’s Manual section about “Order of Execution for Event Functions”&lt;/a&gt;.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Now that our Player Component has the reference to Rigidbody2D stored, we can focus on the logic to move the Game Object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;     // ...

     // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.html
     private Rigidbody2D rb2D;
&lt;span class="gi"&gt;+    // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3-zero.html
+    private Vector3 move = Vector3.zero;
&lt;/span&gt;
     private void Awake()
     {
         // ...
     }

     private void Update()
     {
         // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Input.GetAxisRaw.html
         float rawHorizontalAxis = Input.GetAxisRaw("Horizontal");

-        // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3-zero.html
&lt;span class="gd"&gt;-        Vector3 direction = Vector3.zero;
-        direction.x = rawHorizontalAxis;
-
-        // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Time-deltaTime.html
-        float timeSinceLastFrame = Time.deltaTime;
-
-        Vector3 translation = direction * speed * timeSinceLastFrame;
-
-        // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Transform.Translate.html
-        transform.Translate(
-          translation
-        );
&lt;/span&gt;&lt;span class="gi"&gt;+        move.x = rawHorizontalAxis;
&lt;/span&gt;
         // ...
     }

+    // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.FixedUpdate.html
&lt;span class="gi"&gt;+    private void FixedUpdate()
+    {
+        if (move != Vector3.zero)
+        {
+            // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Time-fixedDeltaTime.html
+            Vector3 translation = move * speed * Time.fixedDeltaTime;
+            Vector3 newPosition = transform.position + translation;
+
+            // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.MovePosition.html
+            rb2D.MovePosition(newPosition);
+        }
+    }
&lt;/span&gt; }

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

&lt;/div&gt;



&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; The syntax shown here is in &lt;a href="https://en.wikipedia.org/wiki/Diff#Unified_format"&gt;&lt;code&gt;diff&lt;/code&gt; unified format&lt;/a&gt; to show replaced code. Due to syntax highlighter limitations, C# syntax colors are disabled.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Let’s explain the changes. We have removed all related old movement logic except the logic in charge of checking for movement input with &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Input.GetAxisRaw.html"&gt;&lt;code&gt;Input.GetAxisRaw&lt;/code&gt;&lt;/a&gt;, added a new private property to &lt;code&gt;Player&lt;/code&gt; class called &lt;code&gt;move&lt;/code&gt; of type &lt;code&gt;Vector3&lt;/code&gt; with a default value of &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3-zero.html"&gt;&lt;code&gt;Vector3.zero&lt;/code&gt;&lt;/a&gt;, then we are updating the property &lt;code&gt;move.x&lt;/code&gt; value in &lt;code&gt;Player&lt;/code&gt;’s &lt;code&gt;Update&lt;/code&gt; method with the value retrieved from the horizontal axis input. We had defined a new method &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.FixedUpdate.html"&gt;&lt;code&gt;FixedUpdate&lt;/code&gt;&lt;/a&gt; to handle physics-related logic. In here, if &lt;code&gt;move&lt;/code&gt; property value differs from &lt;code&gt;Vector3.zero&lt;/code&gt;, it will calculate the Game Object translation based on the &lt;code&gt;move&lt;/code&gt; and the &lt;code&gt;speed&lt;/code&gt; values multiplying it by the time passed (in this case &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Time-fixedDeltaTime.html"&gt;&lt;code&gt;Time.fixedDeltaTime&lt;/code&gt;&lt;/a&gt;). This computed translation is added to the current position. Then we call &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.MovePosition.html"&gt;&lt;code&gt;Rigidbody2D.MovePosition&lt;/code&gt;&lt;/a&gt; method to move the Game Object. This way, the Player Game Object is moved by the Rigidbody2D Component and taken into account b the physics engine.&lt;/p&gt;

&lt;p&gt;If you play the game, there should be no difference in the movement or any noticeable changes. The difference here is that the physics engine works behind the curtains paying attention to each movement of our Player Game Object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bullet Movement with Physics
&lt;/h3&gt;

&lt;p&gt;Now that we have done this with the Player, let’s do it with its Bullets.&lt;/p&gt;

&lt;p&gt;Go to your Project window in the Unity editor and attach a Rigidbody2D Component to Bullet Game Object Prefab. Remember to set it to Kinematic and to check the “Use Full Kinematics Contacts” checkbox. After this, you should play the game. If everything is configured correctly, the bullets should move straight up when shooting.&lt;/p&gt;

&lt;p&gt;Edit &lt;code&gt;Bullet.cs&lt;/code&gt; code and add the &lt;code&gt;RequireComponent&lt;/code&gt; attribute to require &lt;code&gt;Rigidbody2D&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Bullet.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/RequireComponent.html&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;RequireComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Rigidbody2D&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;class&lt;/span&gt; &lt;span class="nc"&gt;Bullet&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="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Add the logic in &lt;code&gt;Bullet.cs&lt;/code&gt; to store the reference to the RigidBody2D Component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Bullet.cs&lt;/code&gt;&lt;/strong&gt;&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;// ...&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bullet&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="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.html&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;rb2D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.Awake.html&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="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Component.GetComponent.html&lt;/span&gt;
        &lt;span class="n"&gt;rb2D&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;// ...&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, update its logic to move using the physics engine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Bullet.cs&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt; ...
 public class Bullet : MonoBehaviour
 {
     // ...

     // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.html
     private Rigidbody2D rb2D;
&lt;span class="gi"&gt;+    // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3-up.html
+    private Vector3 move = Vector3.up;
&lt;/span&gt;
     private void Awake()
     {
         // ...
     }

-    // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.Update.html
&lt;span class="gd"&gt;-    private void Update()
-    {
-        // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3-up.html
-        Vector3 direction = Vector3.up;
-
-        // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Time-deltaTime.html
-        float timeSinceLastFrame = Time.deltaTime;
-
-        Vector3 translation = direction * speed * timeSinceLastFrame;
-
-        // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Transform.Translate.html
-        transform.Translate(
-          translation
-        );
-    }
&lt;/span&gt;&lt;span class="gi"&gt;+    // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.FixedUpdate.html
+    private void FixedUpdate()
+    {
+        if (move != Vector3.zero)
+        {
+            // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Time-fixedDeltaTime.html
+            Vector3 translation = move * speed * Time.fixedDeltaTime;
+            Vector3 newPosition = transform.position + translation;
+
+            // https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.MovePosition.html
+            rb2D.MovePosition(newPosition);
+        }
+    }
&lt;/span&gt; }

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

&lt;/div&gt;



&lt;p&gt;Notice that the &lt;code&gt;Update&lt;/code&gt; method has been removed since it is no longer needed, and we are using the same code as with the Player Component for the &lt;code&gt;FixedUpdated&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;No changes should be noticed when playing the game (physics engine working).&lt;/p&gt;

&lt;h3&gt;
  
  
  Enemies with physics
&lt;/h3&gt;

&lt;p&gt;Last but not least, update the Enemies Game Objects to have a Rigidbody2D of type Kinematic and “Use Full Kinematics Contacts” checkbox checked.&lt;/p&gt;

&lt;p&gt;And update its &lt;code&gt;Enemy.cs&lt;/code&gt; Component code to require and store the reference of &lt;code&gt;Rigidbody2D&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Enemy.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;RequireComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Rigidbody2D&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;class&lt;/span&gt; &lt;span class="nc"&gt;Enemy&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="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Rigidbody2D.html&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;rb2D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.Awake.html&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="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Component.GetComponent.html&lt;/span&gt;
        &lt;span class="n"&gt;rb2D&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="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This one was the hardest. We are not going to implement any movement logic yet on the Enemy.&lt;/p&gt;

&lt;p&gt;Yay! We have finished providing our Game Objects with physic properties and movement. Play the game to ensure everything moves correctly, and nothing falls into the void. It’s time to make things clash.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unity’s Collider2D, Detecting Collisions
&lt;/h2&gt;

&lt;p&gt;Now that Game Objects has physical body representation, the physics engine needs a way to detects collisions between these bodies. This can be made using &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/Collider2D.html"&gt;2D Colliders&lt;/a&gt;. There are several types of Collider2D based in their shape, we will use &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/class-BoxCollider2D.html"&gt;Box Collider 2D&lt;/a&gt; Component given its simplicity and that we do not need complicated shapes yet for the project.&lt;/p&gt;

&lt;p&gt;To make a collision possible, we need 2 Game Objects with Colliders2D attached to them to communicate collisions between them. We are going to make our Bullets collide with Enemies Game Objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bullet to Enemy Collision
&lt;/h3&gt;

&lt;p&gt;Start with the Bullet Game Object Prefab. Open the Bullet Prefab on the Editor by making double click on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q04h6uEJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/05-Unity_Bullet_Prefab_Editor.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q04h6uEJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/05-Unity_Bullet_Prefab_Editor.gif" alt="05-Unity_Bullet_Prefab_Editor" width="880" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add the Box Collider 2D Component, check “Is Trigger” box, this will set the collider to act as a trigger making the physics engine notify the collisions rather than simulate them. Set the Size property to &lt;code&gt;X = 0.125&lt;/code&gt; and &lt;code&gt;Y = 0.25&lt;/code&gt; units. This size makes the Collider area equal in size to the Sprite, the Bullet Sprite have a size of 2x4 pixels, that translated to game units (1 unit -&amp;gt; 16 pixels) is equal to &lt;code&gt;2 / 16 = 0.125&lt;/code&gt; and &lt;code&gt;4 /16 = 0.25&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rR8_MME5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/06-Unity_Inspector_Bullet_Box_Collider_2D.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rR8_MME5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/06-Unity_Inspector_Bullet_Box_Collider_2D.jpg" alt="06-Unity_Inspector_Bullet_Box_Collider_2D" width="511" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do the same for both Enemy Game Objects in the Game Scene, this time the Size is &lt;code&gt;X = 0.5&lt;/code&gt; and &lt;code&gt;Y = 0.5625&lt;/code&gt; (8x9 pixels), and we will need to set Offset property &lt;code&gt;Y = 0.03125&lt;/code&gt; (this moves the collider 0.5 pixels up to center it with the sprite).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--syKkAb1M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/07-Unity_Inspector_Enemy_Box_Collider_2D.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--syKkAb1M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/07-Unity_Inspector_Enemy_Box_Collider_2D.jpg" alt="07-Unity_Inspector_Enemy_Box_Collider_2D.jpg" width="382" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can &lt;strong&gt;copy Components with its property values to other Game Object using the Inspector Window&lt;/strong&gt;. Click on the “3 dots button” at the top left corner of the Enemy’s Box Collider 2D Component, select “Copy Component”, then select the other Enemy Game Object and click on the “3 dots button” at the top left corner of its Transform Component, select “Paste Component As New”. This will attach a new Box Collider 2D Component to the Game Object with the same property values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iJxgq_ga--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/08-Unity_Inspector_Component_Copy_Paste.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iJxgq_ga--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/08-Unity_Inspector_Component_Copy_Paste.gif" alt="08-Unity_Inspector_Component_Copy_Paste.gif" width="752" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have two kinds of Game Objects (Bullets and Enemies) with physical bodies and collision detection, we can handle collisions between them. If you play the game, there shouldn’t be any relevant changes (nothing falls and bullets still pass through).&lt;/p&gt;

&lt;p&gt;Coding time! Edit the Bullet Component script. We are going to add a new method called &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html"&gt;OnTriggerEnter2D&lt;/a&gt;. This method is called whenever a collision is detected by any Collider2D attached to the Game Object with property “Is Trigger” enabled. It will be called when our Bullet Game Objects collide with the Enemy’s Collider2D.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Bullet.cs&lt;/code&gt;&lt;/strong&gt;&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;// ...&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bullet&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="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html&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;OnTriggerEnter2D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Collider2D&lt;/span&gt; &lt;span class="n"&gt;collider2D&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Component.CompareTag.html&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;collider2D&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CompareTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enemy"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Object.Destroy.html&lt;/span&gt;
            &lt;span class="nf"&gt;Destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;collider2D&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gameObject&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="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The Bullet Component is now detecting collisions. To avoid it from destroying every Collider2D it collides with, we have added a condition to check if the Game Object it collides with has the &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/Tags.html"&gt;Tag&lt;/a&gt; “Enemy” and destroying it if so. But wait, if you play the game still nothing happens, we need to tag our Enemy Game Objects with the “Enemy” tag.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unity Tags for Collider Filtering
&lt;/h3&gt;

&lt;p&gt;To set a Tag in our Enemy Game Object, select the Enemy in the Editor and go to the Inspector Window, the Tag select input is placed in the upper part of the Inspector Window. We need to add a new one, to add a new Tag click on the Tag input, and select the “Add Tag…” option, then add the “Enemy” Tag.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lDB41NS4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/09-Unity_Inspector_Add_Enemy_Tag.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lDB41NS4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/09-Unity_Inspector_Add_Enemy_Tag.gif" alt="09-Unity_Inspector_Add_Enemy_Tag" width="483" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now select both Enemy Game Objects and add to them the “Enemy” Tag. If you play the game, you can now kill enemies. Pew! Pew!.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UvI3bgpP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/10-Unity_Game_Bullet_Enemy_Destroy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UvI3bgpP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-03/10-Unity_Game_Bullet_Enemy_Destroy.gif" alt="10-Unity_Game_Bullet_Enemy_Destroy.gif" width="503" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;We have covered a lot in this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Represent Game Objects in Unity as physic bodies to be handled and moved by its 2D Physics engine&lt;/li&gt;
&lt;li&gt;Detect collisions between Game Objects and apply some logic to it&lt;/li&gt;
&lt;li&gt;Manage Tags in a Unity Project and tag Game Objects to be filtered during collision detection logics&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can get the code generated by following this tutorial in &lt;a href="https://github.com/GenomaGames/genoma-invaders/tree/tutorial/03"&gt;Genoma Invaders’ Github repository, branch &lt;code&gt;tutorial/03&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Remember to dig into documentation sites to have more context on what does what:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/index.html"&gt;Unity 2020.1 Manual&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/index.html"&gt;Unity 2020.1 Script Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/"&gt;Microsoft .NET C# Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy Game Dev! 👾&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>gamedev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Getting Started in Unity 2D Game Development</title>
      <dc:creator>Alberto Fernandez Medina</dc:creator>
      <pubDate>Sun, 02 Aug 2020 16:28:53 +0000</pubDate>
      <link>https://dev.to/genomagames/getting-started-in-unity-2d-game-development-583k</link>
      <guid>https://dev.to/genomagames/getting-started-in-unity-2d-game-development-583k</guid>
      <description>&lt;p&gt;This tutorial covers some basic concepts about start developing 2D games in Unity. Game Object, Components, Sprites, how to handle Pixel Art for games in Unity, and helps to get used to Unity editor interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;To follow this Unity tutorial, you will need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://unity3d.com/es/get-unity/download"&gt;Install UnityHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://genomagames.com/tutorials/unity/2020/07/02/how-to-install-unity-2020/"&gt;Install Unity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://genomagames.com/tutorials/unity/2020/07/16/how-to-create-a-unity-2d-project/"&gt;Create a new 2D Game Unity Project&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/UsingTheEditor.html"&gt;Have basic knowledge about Unity's interface&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Genoma Invaders Game
&lt;/h2&gt;

&lt;p&gt;The game we will be developing during this tutorial series is &lt;strong&gt;Genoma Invaders&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Genoma Invaders is a &lt;a href="https://en.wikipedia.org/wiki/Category:Fixed_shooters"&gt;Fixed shooter&lt;/a&gt; from the &lt;a href="https://en.wikipedia.org/wiki/Shoot_%27em_up"&gt;Shoot 'em up&lt;/a&gt; game genre where you are a microscopic submarine killing bacteria, virus, and other microorganisms inside a human body.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To define this project and its scope, a very simple &lt;a href="https://github.com/GenomaGames/genoma-invaders/tree/tutorial/01/docs/game-design-document.md"&gt;Game Design Document&lt;/a&gt; has been created.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Don't use this project as an example for game design as there is not much design process behind this project due to its educational purpose&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project File Structure
&lt;/h2&gt;

&lt;p&gt;It is not mandatory, but a way to keep things tidy is to create a new directory inside &lt;code&gt;/Assets&lt;/code&gt; with the name of your game, in this case, &lt;code&gt;/Assets/Genoma Invaders&lt;/code&gt;, and move the &lt;code&gt;Scenes&lt;/code&gt; directory inside of it. This way, all the game-related assets are contained inside this directory so they do not get mixed with 3rd party assets or plugins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the First Game Scene
&lt;/h2&gt;

&lt;p&gt;Let's start with the &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/CreatingScenes.html"&gt;&lt;strong&gt;Scene&lt;/strong&gt;&lt;/a&gt; Unity creates whenever you create a new project by renaming it from &lt;code&gt;SampleScene&lt;/code&gt; to &lt;code&gt;Game&lt;/code&gt;. Here is where all the game action will happen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WNrMBVP8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/01-Unity_2020.1_blank_2D_project.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WNrMBVP8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/01-Unity_2020.1_blank_2D_project.png" alt="01-Unity_2020_blank_2D_project" width="880" height="585"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every game in Unity is contained within one or multiple scenes. For this project, we will be using them to store one level in each one and the main menu in another one, but for now, we only need one to start our game development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Game Objects
&lt;/h3&gt;

&lt;p&gt;The next item smaller than a Unity scene is a &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/GameObjects.html"&gt;&lt;strong&gt;Game Object&lt;/strong&gt;&lt;/a&gt;. They comprehend the bricks to build a game, graphics, sounds, and logic. A Game Object can represent multiple things in a Unity game, players, enemies, lights, the ground, trees, cameras. These objects can also represent things that the player can not see, like the camera itself or an object in charge of reviving the player whenever it dies.&lt;/p&gt;

&lt;p&gt;Unity starts a project always by placing a &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/class-Camera.html"&gt;&lt;strong&gt;Camera&lt;/strong&gt;&lt;/a&gt; Game Object inside the Scene (depending on the project template it could put more stuff). A Camera is essential since it represents the player's eyes as they will see what the camera sees. Do not mind it too much since we will not be handling it yet.  &lt;/p&gt;

&lt;p&gt;A Game Object will represent the player. To create Game Objects in the Scene, click on the menu &lt;code&gt;GameObject &amp;gt; 2D Object &amp;gt; Sprite&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7TzwNuPN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/02-Unity_GameObject_2D_Object_Sprite_menu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7TzwNuPN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/02-Unity_GameObject_2D_Object_Sprite_menu.png" alt="02-Unity_GameObject_2D_Object_Sprite_menu" width="667" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will create a new Game Object in the Scene called &lt;code&gt;New Sprite&lt;/code&gt;. Rename it to &lt;code&gt;Player&lt;/code&gt; and ensure its position is in &lt;code&gt;X=0 Y=0 Z=0&lt;/code&gt; in the &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/UsingTheInspector.html"&gt;&lt;strong&gt;Inspector window&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_kvNdx89--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/03-Unity_Inspector_Player_Game_Object.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_kvNdx89--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/03-Unity_Inspector_Player_Game_Object.png" alt="03-Unity_Inspector_Player_Game_Object" width="448" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Components
&lt;/h3&gt;

&lt;p&gt;This Game Object we renamed &lt;code&gt;Player&lt;/code&gt; should show a &lt;strong&gt;Sprite Renderer&lt;/strong&gt; in the Inspector window.&lt;/p&gt;

&lt;p&gt;Each section in the Inspector window, divided by a line and a header, represents a &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/Components.html"&gt;&lt;strong&gt;Component&lt;/strong&gt;&lt;/a&gt; which is the next piece smaller than a Game Object. Game Objects contains Components that provide different features to the Game Object, the most common Component you can find in a Game Object is the &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/class-Transform.html"&gt;&lt;strong&gt;Transform&lt;/strong&gt;&lt;/a&gt; Component. It gives the Game Object the ability to be positioned, rotated, or scaled inside the Scene, so if the Game Object renders something, it will be placed based on that values or could calculate its distance against other entities in the Scene.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sprites
&lt;/h3&gt;

&lt;p&gt;Time to inject some graphics into the game. Take this &lt;a href="https://genomagames.com/genoma-invaders/assets/lesson-01/Player.png"&gt;Player sprite&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rzhh180j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/Player.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rzhh180j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/Player.png" alt="Player" width="16" height="16"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And put it into the project. You can do it by dragging it into the &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/ProjectView.html"&gt;&lt;strong&gt;Project window&lt;/strong&gt;&lt;/a&gt; or placing it with your operating system file explorer into the project &lt;code&gt;Assets&lt;/code&gt; directory. Place it under &lt;code&gt;Assets/Genoma Invaders/Player/Player.png&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let assign the Player sprite to the Player Game Object in the Scene:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select the Player in the Scene &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/Hierarchy.html"&gt;&lt;strong&gt;Hierarchy window&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;In the Inspector window, on the &lt;strong&gt;Sprite Renderer&lt;/strong&gt; component, click on the circle of the left to the Sprite field to open the &lt;strong&gt;Select Sprite&lt;/strong&gt; window&lt;/li&gt;
&lt;li&gt;Select the Player sprite on the Select Sprite window&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mVu55K-B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/04-Unity_Inspector_Sprite_Selection.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mVu55K-B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/04-Unity_Inspector_Sprite_Selection.png" alt="04-Unity_Inspector_Sprite_Selection" width="880" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the Player entity is visible within the game Camera. To check this, go to the &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/GameView.html"&gt;&lt;strong&gt;Game view&lt;/strong&gt;&lt;/a&gt; view, you should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nG8h9Blz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/05-Unity_Game_Panel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nG8h9Blz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/05-Unity_Game_Panel.png" alt="05-Unity_Game_Panel" width="594" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That little dot is our &lt;strong&gt;Player&lt;/strong&gt; rendered by the camera. The reason it is so small is that the size of the sprite is 16x16 pixels. Sprites, Cameras, and settings in Unity need to be configured to render &lt;a href="https://en.wikipedia.org/wiki/Pixel_art"&gt;Pixel Art&lt;/a&gt; correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pixel Art in Unity
&lt;/h2&gt;

&lt;p&gt;To render pixel art correctly in Unity, we need to make a few changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unity Texture 2D Import Settings for Pixel Art
&lt;/h3&gt;

&lt;p&gt;The primary problem Unity has with Pixel Art is that it treats the images used for the Game Objects as Textures, and it applies some optimization over them to improve load and render times. The problem is that that optimization is useful only if we are working with large resolution images that need to be compressed or optimized. In the case of Pixel Art, these optimizations are not required since these graphics are so small in size. &lt;/p&gt;

&lt;p&gt;To configure Player Sprite with proper settings, select it on the Project window to display its settings on the Inspector window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i239v8EP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/06-Unity_Inspector_window_Texture_2D_Import_Settings.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i239v8EP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/06-Unity_Inspector_window_Texture_2D_Import_Settings.png" alt="06-Unity_Inspector_window_Texture_2D_Import_Settings" width="880" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pixels Per Unit&lt;/strong&gt;: 16 (It will make the sprite bigger in the Scene because when applying this value, it means that for this sprite 16 pixels will have to be rendered inside 1 game world unit)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filter Mode&lt;/strong&gt;: Point (no filter) (Will prevent blurriness with the pixels since that option is in charge of filtering the texture when it gets stretched)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compression&lt;/strong&gt;: None (This prevents Unity from compressing the image that also will cause blurriness)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Press the &lt;strong&gt;Apply&lt;/strong&gt; button to save the changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1LyiO9Jv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/07-Unity_Inspector_window_Import_Settings_for_Pixel_Art_16_pixels.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1LyiO9Jv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/07-Unity_Inspector_window_Import_Settings_for_Pixel_Art_16_pixels.png" alt="07-Unity_Inspector_window_Import_Settings_for_Pixel_Art_16_pixels" width="442" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; This process needs to be made with every sprite included in the project.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;If you look now in the Game view, the Player Sprite should look bigger.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GilbHCXx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/08-Unity_Game_view_pixel_player.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GilbHCXx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/08-Unity_Game_view_pixel_player.png" alt="08-Unity_Game_view_pixel_player" width="526" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2D Pixel Perfect Package
&lt;/h3&gt;

&lt;p&gt;Another tweak we can do to help the Unity project render pixel graphics is to install the &lt;a href="https://docs.unity3d.com/Packages/com.unity.2d.pixel-perfect@3.0/manual/index.html"&gt;&lt;strong&gt;2D Pixel Perfect&lt;/strong&gt;&lt;/a&gt; package.&lt;/p&gt;

&lt;h4&gt;
  
  
  2D Pixel Perfect Package Installation
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;If you created the project selecting the 2D Project Template in Unity Hub you already have this package installed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To install the package, open the &lt;strong&gt;Package Manager&lt;/strong&gt; window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iYpkLByt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/09-Unity_Window_menu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iYpkLByt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/09-Unity_Window_menu.png" alt="09-Unity_Window_menu" width="563" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set &lt;strong&gt;Packages&lt;/strong&gt; option to show &lt;strong&gt;All&lt;/strong&gt; packages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tYlw0vtk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/10-Unity_Package_Manager_window_Package_filter.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tYlw0vtk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/10-Unity_Package_Manager_window_Package_filter.png" alt="10-Unity_Package_Manager_window_Package_filter" width="203" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the &lt;strong&gt;2D Pixel Perfect&lt;/strong&gt; package and the press the &lt;strong&gt;Install&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YzcOQhrM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/11-Unity_Package_Manager_2D_Pixel_Perfect_Install.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YzcOQhrM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/11-Unity_Package_Manager_2D_Pixel_Perfect_Install.png" alt="11-Unity_Package_Manager_2D_Pixel_Perfect_Install" width="804" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  2D Pixel Perfect Package's Pixel Camera
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;2D Pixel Perfect&lt;/strong&gt; package comes with a &lt;strong&gt;Component&lt;/strong&gt; called &lt;strong&gt;Pixel Camera&lt;/strong&gt;. Unity Cameras are not prepared to render pixel graphics correctly, and this Component provides Unity's Cameras with behavior to handle pixel viewport logics and other features to help your game look more pixelated.&lt;/p&gt;

&lt;p&gt;Select the &lt;strong&gt;Main Camera&lt;/strong&gt; in the Hierarchy window to show its Inspector window. To add the &lt;strong&gt;Pixel Camera&lt;/strong&gt; Component to the Main Camera, click on the &lt;strong&gt;Add Component&lt;/strong&gt; button. Go to &lt;strong&gt;Rendering&lt;/strong&gt; and then select &lt;strong&gt;Pixel Perfect Camera&lt;/strong&gt; (you can also find it using the search input).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E1gzJuEk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/12-Unity_Add_Component_Pixel_Perfect_Camera.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E1gzJuEk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/12-Unity_Add_Component_Pixel_Perfect_Camera.png" alt="12-Unity_Add_Component_Pixel_Perfect_Camera" width="230" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the Main Camera has the Pixel Perfect Camera Component, edit its parameters.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Assets Pixels Per Unit:&lt;/strong&gt; 16 (the same value we used for the Player Sprite)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reference Resolution:&lt;/strong&gt; 224x256 (you'll know why these values later)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Upscale Render Texture:&lt;/strong&gt; checked&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Crop Frame:&lt;/strong&gt; X and Y checked&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xM06wk3L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/13-Unity_Pixel_Perfect_Camera_Component.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xM06wk3L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/13-Unity_Pixel_Perfect_Camera_Component.png" alt="13-Unity_Pixel_Perfect_Camera_Component" width="367" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; checkout &lt;a href="https://docs.unity3d.com/Packages/com.unity.2d.pixel-perfect@3.0/manual/index.html"&gt;2D Pixel Perfect package documentation&lt;/a&gt; for more info.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Arcade Cabinet Taito 8080
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Space_Invaders"&gt;Space Invaders&lt;/a&gt; were played in an &lt;a href="https://en.wikipedia.org/wiki/Arcade_cabinet"&gt;arcade cabinet&lt;/a&gt; with a &lt;a href="https://www.system16.com/hardware.php?id=629"&gt;Taito 8080&lt;/a&gt; motherboard which could render a resolution of 224x256 pixels.&lt;/p&gt;

&lt;p&gt;Unity can simulate resolutions and aspect ratios freely. Let's begin by setting up a new &lt;strong&gt;Aspect&lt;/strong&gt; configuration on the Game view:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aFj8D5J9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/14-Unity_Game_view_aspect_configuration_menu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aFj8D5J9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/14-Unity_Game_view_aspect_configuration_menu.png" alt="14-Unity_Game_view_aspect_configuration_menu" width="595" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To simulate a Taito arcade machine create a mew aspect with 224 with and 256 height:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ba0WryyE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/15-Unity_Game_view_new_aspect_form.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ba0WryyE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/15-Unity_Game_view_new_aspect_form.png" alt="15-Unity_Game_view_new_aspect_form" width="263" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the new aspect is selected, the Game view will change to represent this resolution. To try how the game looks under this resolution, press the &lt;strong&gt;Play&lt;/strong&gt; button and select it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vSVoYI6F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/16-Unity_Game_view_aspect_selected.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vSVoYI6F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/16-Unity_Game_view_aspect_selected.png" alt="16-Unity_Game_view_aspect_selected" width="515" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stop the game by pressing the &lt;strong&gt;Play&lt;/strong&gt; button again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enemies
&lt;/h2&gt;

&lt;p&gt;Let's make the same process with this &lt;a href="https://genomagames.com/genoma-invaders/assets/lesson-01/Enemy.png"&gt;Enemy sprite&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BjL5O9hM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/Enemy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BjL5O9hM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/Enemy.png" alt="Enemy" width="16" height="16"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Place it under &lt;code&gt;/Assets/Genoma Invaders/Enemy/Enemy.png&lt;/code&gt; and update its settings as we did with the Player sprite.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pixels Per Unit: 16&lt;/li&gt;
&lt;li&gt;Filter Mode: Point (no filter)&lt;/li&gt;
&lt;li&gt;Compression: None&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, to create the Enemy Game Object in the Scene, there is another way to create it. Drag and drop the Enemy Sprite from the Project window to the Hierarchy window, this will create a new Game Object with a Sprite Renderer Component called "Enemy" in the Scene. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hUdWyIic--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/17-Unity_Editor_create_sprite_object_in_scene.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hUdWyIic--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/17-Unity_Editor_create_sprite_object_in_scene.png" alt="17-Unity_Editor_create_sprite_object_in_scene" width="880" height="723"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have two visible game objects in the Scene let's place them where they suppose to be in the game. Move the Player and the Enemy to be positioned similar to this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NowRNuj3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/18-Unity_Editor_Scene_Game_Object_move.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NowRNuj3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/18-Unity_Editor_Scene_Game_Object_move.png" alt="18-Unity_Editor_Scene_Game_Object_move" width="456" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now select the Enemy Game Object and press Ctrl/Cmd + D, in the Hierarchy should appear an &lt;code&gt;Enemy (1)&lt;/code&gt; Game Object, you have just duplicated a Game Object, place it next to its partner, and you should end with something that starts to smell like a game.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gA_Fq2NY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/19-Unity_Editor_Scene_Game_Object_duplication.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gA_Fq2NY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-01/19-Unity_Editor_Scene_Game_Object_duplication.png" alt="19-Unity_Editor_Scene_Game_Object_duplication" width="510" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap Up
&lt;/h2&gt;

&lt;p&gt;We have seen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to import Sprites in a Unity project&lt;/li&gt;
&lt;li&gt;How to create Game Objects in Unity&lt;/li&gt;
&lt;li&gt;How to add components to a Game Object in Unity&lt;/li&gt;
&lt;li&gt;How to adapt a Unity project, its camera, and sprites to be Pixel Art friendly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is all for now.&lt;/p&gt;

&lt;p&gt;The resulting work of this tutorial can be checked out in the &lt;a href="https://github.com/GenomaGames/genoma-invaders/tree/tutorial/01"&gt;Genoma Invaders Github, branch &lt;code&gt;tutorial/01&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Game Dev! 👾&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>unity3d</category>
      <category>gamedev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Move 2D Objects in Unity</title>
      <dc:creator>Alberto Fernandez Medina</dc:creator>
      <pubDate>Fri, 31 Jul 2020 22:00:00 +0000</pubDate>
      <link>https://dev.to/genomagames/how-to-move-2d-objects-in-unity-573n</link>
      <guid>https://dev.to/genomagames/how-to-move-2d-objects-in-unity-573n</guid>
      <description>&lt;p&gt;This tutorial shows &lt;strong&gt;basics for Unity 2D Game Development concepts like adding custom components to Game Object, fundamental Unity scripting with C#, 2D Game Object movement in Unity, how to handle inputs in Unity, and how to instantiate Game Object in Unity&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;Due to this course's intention, we will not cover &lt;strong&gt;basic knowledge in programming with C# in Unity&lt;/strong&gt; but rather provide info with complex terms in this language or Unity Scripting API.&lt;/p&gt;

&lt;p&gt;To get more info and learn about these topics, you can refer to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/"&gt;Microsoft's .NET C# Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/CreatingAndUsingScripts.html"&gt;Unity's Manual section "Creating and Using Scripts"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/index.html"&gt;Unity API Scripting Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.codecademy.com/learn/learn-c-sharp"&gt;Codecademy's Learn C# Course&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learn.unity.com/"&gt;Unity Learn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Complete &lt;a href="https://dev.to/genomagames/getting-started-in-unity-2d-game-development-583k"&gt;"&lt;strong&gt;Getting Started in Unity 2D Game Development&lt;/strong&gt;"&lt;/a&gt; or checkout the code from &lt;a href="https://github.com/GenomaGames/genoma-invaders/tree/tutorial/01"&gt;Genoma Invaders' Github repository, branch &lt;code&gt;tutorial/01&lt;/code&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Have &lt;strong&gt;basics knowledge in programming with &lt;a href="https://en.wikipedia.org/wiki/C_Sharp_(programming_language)"&gt;C#&lt;/a&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://visualstudio.microsoft.com/es/vs/"&gt;&lt;strong&gt;Visual Studio Editor&lt;/strong&gt;&lt;/a&gt; (you should have installed it when installing Unity) or another code editor of your preference&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the previous tutorial, &lt;a href="https://dev.to/genomagames/getting-started-in-unity-2d-game-development-583k"&gt;"Getting Started in Unity 2D Game Development"&lt;/a&gt;, we walk through basic concepts about developing 2D games in Unity, ending up with a scene with some still Game Objects. It’s time to give them some movement logic starting by the Player.&lt;/p&gt;

&lt;p&gt;This time we will &lt;strong&gt;learn to move 2D Object in Unity by scripting in C# using Game Components&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Components
&lt;/h2&gt;

&lt;p&gt;To move our Player, we will need to add some logic to its dumb Game Object. We will do it by creating a Custom Component containing C# code and adding it to the Player Game Object.&lt;/p&gt;

&lt;p&gt;Select the Player Game Object and press the “Add Component” button in the Inspector window. Then, write in the search input &lt;code&gt;Player&lt;/code&gt;, select &lt;code&gt;New Script&lt;/code&gt;, and press the “Create and Add” button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qh15poW4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/01-Unity_Add_Player_Component.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qh15poW4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/01-Unity_Add_Player_Component.gif" alt="01-Unity_Add_Player_Component" width="413" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; checkout &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/CreatingComponents.html"&gt;Unity’s Manual section “Creating components with scripting”&lt;/a&gt; for more info.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;This action will create a C# file under &lt;code&gt;/Assets/Player.cs&lt;/code&gt; that defines a component named “Player”, and then it will add this Component to the Player Game Object. Move the &lt;code&gt;Player.cs&lt;/code&gt; file to &lt;code&gt;/Assets/Genoma Invaders/Player/&lt;/code&gt; directory using the Project window to maintain the project’s files organized.&lt;/p&gt;

&lt;h2&gt;
  
  
  Player Controller
&lt;/h2&gt;

&lt;p&gt;The Player component will be the Player Controller or, in other words, the Component in charge of handle the player’s logic.&lt;/p&gt;

&lt;p&gt;If you open the file you’ll see this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections.Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&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;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="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="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;That is the basic code structure Unity Editor creates for every script/component created in the project.&lt;/p&gt;

&lt;p&gt;Now we are going to make de same, and &lt;strong&gt;create an Enemy Custom Component for the Enemy Game Objects&lt;/strong&gt;. This time we are using a different technique. On the Project window, go to &lt;code&gt;/Assets/Genoma Games/Enemy&lt;/code&gt; right click on it to open the contextual menu and select &lt;code&gt;Create &amp;gt; C# Script&lt;/code&gt;, then name the file &lt;code&gt;Enemy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pmZsfxIb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/02-Unity_Project_create_new_csharp_script.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pmZsfxIb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/02-Unity_Project_create_new_csharp_script.gif" alt="02-Unity_Project_create_new_csharp_script" width="880" height="601"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;Enemy.cs&lt;/code&gt; file is created, Unity reloads for a sort time and will register a new component to be added to Game Object named &lt;code&gt;Enemy&lt;/code&gt;. Go ahead and add it to both Enemy Game Objects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6pYU8HI---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/03-Unity_Inspector_add_enemy_component.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6pYU8HI---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/03-Unity_Inspector_add_enemy_component.gif" alt="04-Unity_Inspector_add_enemy_component" width="399" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Remember to add the Enemy component to both enemies.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Game Object Movement
&lt;/h2&gt;

&lt;p&gt;To make the Player Game Object move, edit &lt;code&gt;Player.cs&lt;/code&gt; file. First, we will clean it up a bit, so it doesn’t have unnecessary code:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&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;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="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="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;Now, we will add some logic into its Player Controller’s &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/MonoBehaviour.Update.html"&gt;&lt;code&gt;Update&lt;/code&gt;&lt;/a&gt; loop method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&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;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="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="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3-right.html&lt;/span&gt;
        &lt;span class="n"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Vector3&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;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Time-deltaTime.html&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeSinceLastFrame&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;translation&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;span class="n"&gt;timeSinceLastFrame&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Transform.Translate.html&lt;/span&gt;
        &lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="n"&gt;translation&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;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Visit any of the links in the code’s comments for more info.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;This code is creating a &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3.html"&gt;&lt;code&gt;Vector3&lt;/code&gt;&lt;/a&gt; pointing to the right (1, 0, 0) and multiplying it by time elapsed since the last frame, and then calls &lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Transform.Translate.html"&gt;&lt;code&gt;Transform.Translate&lt;/code&gt;&lt;/a&gt; method with the resulting Vector3 which will move the Game Object that distance.&lt;/p&gt;

&lt;p&gt;TL;DR; This will make the player move 1 Scene unit per second to the right.&lt;/p&gt;

&lt;p&gt;To see the result go to Unity Editor and press the “Play” button (You can use the keyboard shortcut Ctrl/Cmd+P).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p26nnb21--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/04-Unity_Editor_Player_moving_right.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p26nnb21--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/04-Unity_Editor_Player_moving_right.gif" alt="04-Unity_Editor_Player_moving_right" width="418" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s alive!!! But we need to be able to control this little guy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Detect Player Input
&lt;/h2&gt;

&lt;p&gt;To control the Player, we need to use Unity’s &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/class-InputManager.html"&gt;Input Manager&lt;/a&gt;. With the &lt;a href="https://docs.unity3d.com/ScriptReference/Input.GetAxisRaw.html"&gt;&lt;code&gt;Input.GetAxisRaw&lt;/code&gt;&lt;/a&gt; method, you can retrieve the corresponding axis input value, in this case, if some left or right key is pressed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&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;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="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="c1"&gt;// https://docs.unity3d.com/ScriptReference/Input.GetAxisRaw.html&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;rawHorizontalAxis&lt;/span&gt; &lt;span class="p"&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;GetAxisRaw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Horizontal"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3-zero.html&lt;/span&gt;
        &lt;span class="n"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Vector3&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;direction&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;rawHorizontalAxis&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Time-deltaTime.html&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeSinceLastFrame&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;timeSinceLastFrame&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Transform.Translate.html&lt;/span&gt;
        &lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="n"&gt;translation&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;Now you can control the player.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z-awhnAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/05-Unity_Editor_Player_moving_input.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z-awhnAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/05-Unity_Editor_Player_moving_input.gif" alt="05-Unity_Editor_Player_moving_input" width="418" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem is, it is a bit slow. To fix this, we are going to add one more value to our player logic. Edit the Player component code, add a &lt;code&gt;speed&lt;/code&gt; property to it, and include this value in the &lt;code&gt;translation&lt;/code&gt; calculus.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&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;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="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;2&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;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;

        &lt;span class="n"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&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;timeSinceLastFrame&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&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;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Some code has been hidden under &lt;code&gt;// ...&lt;/code&gt; to show only applied changed.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Now the Player should move twice as fast if you try it. But there is one small problem here if we want to change our Player speed value we need to open its code and edit the value and then wait for Unity to recompile. Unity provides various ways to allow the exposure of properties in the editor so you can tweak as many values as you want without editing any code. You could set the property to be &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers"&gt;&lt;code&gt;public&lt;/code&gt;&lt;/a&gt; (which I don’t recommend to enforce &lt;a href="https://en.wikipedia.org/wiki/SOLID"&gt;code cleanliness&lt;/a&gt;), or you could add the &lt;a href="https://docs.unity3d.com/ScriptReference/SerializeField.html"&gt;&lt;code&gt;SerializeField&lt;/code&gt;&lt;/a&gt; attribute to the Player’s &lt;code&gt;speed&lt;/code&gt; property, we will do the second.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&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;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="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;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

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

&lt;/div&gt;



&lt;p&gt;Now, if you go to the Unity Editor and checkout the Player Component at the Inspector window, you’ll see that now the Component has a new input called &lt;code&gt;Speed&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kYo2kSyL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/06-Unity_Inspector_Player_Component_with_Speed_input.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kYo2kSyL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/06-Unity_Inspector_Player_Component_with_Speed_input.jpg" alt="06-Unity_Inspector_Player_Component_with_Speed_input" width="398" height="74"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the &lt;code&gt;2&lt;/code&gt; we set for the &lt;code&gt;speed&lt;/code&gt; property will be a default value when the Component is added to a Game Object, and the final used value for the logic will be the one set at the editor. You can edit it even while playing to adjust it to your preferred value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OteTs15x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/07-Unity_Game_property_edit.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OteTs15x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/07-Unity_Game_property_edit.gif" alt="07-Unity_Game_property_edit" width="693" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Properties changed during Play mode will not be saved and will reset to its value before stating Play mode. More info in Unity’s Manual section “&lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/GameView.html"&gt;Game View&lt;/a&gt;“&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Instantiate Game Objects
&lt;/h2&gt;

&lt;p&gt;Time to make our Player shoot some bullets. Download this &lt;a href="https://genomagames.com/genoma-invaders/assets/lesson-02/Bullet.png"&gt;Bullet sprite&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WMpzyNd_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/Bullet.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WMpzyNd_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/Bullet.png" alt="Bullet" width="16" height="16"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And place it under &lt;code&gt;/Assets/Bullet/&lt;/code&gt; (new directory). Remember to edit the Sprite settings as we did with the Player and Enemy sprites in the &lt;a href="https://dev.to/genomagames/getting-started-in-unity-2d-game-development-583k"&gt;previous tutorial&lt;/a&gt;. Then use it to create a new Game Object in the Scene called Bullet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bRgOlBcc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/08-Unity_Editor_create_bullet_game_object.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bRgOlBcc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/08-Unity_Editor_create_bullet_game_object.gif" alt="08-Unity_Editor_create_bullet_game_object" width="692" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This Bullet Game Object will need custom logic to move and do stuff, so let’s create a new C# Script called Bullet and add its Component to the Bullet Game Object. Edit the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Bullet.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&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;class&lt;/span&gt; &lt;span class="nc"&gt;Bullet&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="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;5&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;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Vector3-up.html&lt;/span&gt;
        &lt;span class="n"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Time-deltaTime.html&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeSinceLastFrame&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&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;timeSinceLastFrame&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Transform.Translate.html&lt;/span&gt;
        &lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="n"&gt;translation&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;We have added similar logic to our Bullet Game Object as we did to the player, but this time it only moves upwards and with a higher speed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M4PRMBsF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/09-Unity_Game_bullet_moving.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M4PRMBsF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/09-Unity_Game_bullet_moving.gif" alt="09-Unity_Game_bullet_moving" width="597" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem now is that we only have one Bullet, and it appears and starts moving whenever we start playing. We need it only to appear when we are pressing the fire button. To achieve this, we will need to create a &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/Prefabs.html"&gt;Prefab&lt;/a&gt; from our Bullet Game Object.&lt;/p&gt;

&lt;p&gt;To &lt;strong&gt;create a Unity Prefab&lt;/strong&gt; from an already existing Game Object, you need to drag and drop it to the Project window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--as6DVDj4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/10-Unity_Editor_create_bullet_prefab.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--as6DVDj4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/10-Unity_Editor_create_bullet_prefab.gif" alt="10-Unity_Editor_create_bullet_prefab" width="560" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Prefab is like a blueprint of a Game Object. It allows us to create multiple copies of the same Game Object without having them in the Scene. If you take a look at the Hierarchy window, you will see that the Bullet Game Object now has a different color, icon, and a little arrow to the right, this is the way Unity indicates if a Game Object in the Scene is related with a Prefab. Delete this Game Object from the Scene.&lt;/p&gt;

&lt;p&gt;Check the Bullet Prefab properties within the Inspector window and ensure that it has Transform.position to &lt;code&gt;x=0 y=0 z=0&lt;/code&gt; and Bullet &lt;code&gt;speed=0&lt;/code&gt;. We need to update the Player’s logic to instantiate this Game Object whenever we press the “Fire1” button (which is mapped to &lt;code&gt;left ctrl&lt;/code&gt;, &lt;code&gt;mouse 0&lt;/code&gt; and &lt;code&gt;joystick button 0&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Checkout &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/class-InputManager.html"&gt;Unity’s Manual section “Input Manager”&lt;/a&gt; to know more on how to configure input mappings.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Player.cs&lt;/code&gt;&lt;/strong&gt;&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="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&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;class&lt;/span&gt; &lt;span class="nc"&gt;Player&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="c1"&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;GameObject&lt;/span&gt; &lt;span class="n"&gt;bullet&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;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;

        &lt;span class="c1"&gt;// https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html&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;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetButtonDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fire1"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;playerPosition&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;transform&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="c1"&gt;// https://docs.unity3d.com/2020.1/Documentation/ScriptReference/Quaternion-identity.html&lt;/span&gt;
            &lt;span class="nf"&gt;Instantiate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;playerPosition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Quaternion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;identity&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="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;small&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Some code has been hidden under &lt;code&gt;// ...&lt;/code&gt; to show only applied changed.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;We have defined a new Player Component property called &lt;code&gt;bullet&lt;/code&gt; that will store the reference to the Bullet Prefab Game Object. Then we have updated the &lt;code&gt;Update&lt;/code&gt; with more code to check if any input mapped to the button “Fire1” has been pressed down during this frame, if it has, then the script will instantiate a new Bullet Game Object in the Scene at the Player’s position (with no rotation). Select the Player Game Object in the Scene and update its properties in the Inspector window dragging the Bullet prefab to its Bullet property in the Player Component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xoczX7j2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/11-Unity_Editor_assign_bullet_to_player_bullet.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xoczX7j2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/11-Unity_Editor_assign_bullet_to_player_bullet.gif" alt="11-Unity_Editor_assign_bullet_to_player_bullet" width="560" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you are ready to fire!!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yAqSAna8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/12-Unity_Game_player_shooting_bullets.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yAqSAna8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://genomagames.com/genoma-invaders/assets/lesson-02/12-Unity_Game_player_shooting_bullets.gif" alt="12-Unity_Game_player_shooting_bullets" width="313" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Summing Up we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a new custom Player Component made with C# to our Player Game Object&lt;/li&gt;
&lt;li&gt;Implement moving mechanics to the Player and Bullet Game Objects&lt;/li&gt;
&lt;li&gt;Handle Player Inputs to move and shoot Bullets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can get the code generated by following this tutorial in &lt;a href="https://github.com/GenomaGames/genoma-invaders/tree/tutorial/02"&gt;Genoma Invaders Github repository, branch &lt;code&gt;tutorial/02&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Remember to dig into documentation sites to have more context on what does what:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/index.html"&gt;Unity 2020.1 Manual&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.unity3d.com/2020.1/Documentation/ScriptReference/index.html"&gt;Unity 2020.1 Script Reference&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/"&gt;Microsoft .NET C# Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy Game Dev! 👾&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>gamedev</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How To Install Unity 2020.1</title>
      <dc:creator>Alberto Fernandez Medina</dc:creator>
      <pubDate>Wed, 01 Jul 2020 22:00:00 +0000</pubDate>
      <link>https://dev.to/genomagames/how-to-install-unity-2020-1-43ci</link>
      <guid>https://dev.to/genomagames/how-to-install-unity-2020-1-43ci</guid>
      <description>&lt;p&gt;In this article, you will &lt;strong&gt;learn how to install Unity 2020.1.0b13 (Beta)&lt;/strong&gt; using UnityHub 2.3.2 to &lt;strong&gt;start developing games&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; &lt;a href="https://blogs.unity3d.com/es/2020/07/23/unity-2020-1-is-now-available/"&gt;Unity 2020.1 is no longer in beta&lt;/a&gt; but you can still use this guide for stable releases&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;p&gt;To install Unity, you will need first to install &lt;a href="https://unity3d.com/es/get-unity/download"&gt;UnityHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unity Installation
&lt;/h2&gt;

&lt;p&gt;Now to &lt;strong&gt;install Unity&lt;/strong&gt; start UnityHub and go to &lt;strong&gt;Installs&lt;/strong&gt; section and then click on the &lt;strong&gt;ADD&lt;/strong&gt; button. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ph9wOdBz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/00-UnityHub_2.3.2_Add_Install_Button.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ph9wOdBz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/00-UnityHub_2.3.2_Add_Install_Button.png" alt="00-UnityHub_2.3.2_Add_Install_Button" width="880" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then select the Unity version you would like to install, in this case &lt;strong&gt;Unity 2020.1.0b13 (Beta)&lt;/strong&gt; and click &lt;strong&gt;NEXT&lt;/strong&gt;. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SadPNP9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/01-UnityHub_2.3.2_Install_Unity_Panel_with_Unity_2020.1.0b13_%28Beta%29_selected.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SadPNP9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/01-UnityHub_2.3.2_Install_Unity_Panel_with_Unity_2020.1.0b13_%28Beta%29_selected.png" alt="01-UnityHub_2.3.2_Install_Unity_Panel_with_Unity_2020.1.0b13_(Beta)_selected" width="880" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check the box of &lt;strong&gt;&lt;a href="https://visualstudio.microsoft.com/es/vs/"&gt;Microsoft Visual Studio Community 2019&lt;/a&gt;&lt;/strong&gt; to install it as it will be the &lt;a href="https://en.wikipedia.org/wiki/Integrated_development_environment"&gt;IDE&lt;/a&gt; you will be using for coding. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6ZgqCI0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/02-UnityHub_2.3.2_Install_Unity_2020.1.0b13_%28Beta%29_Panel_with_Microsoft_Visual_Studio_Community_2019_selected.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6ZgqCI0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/02-UnityHub_2.3.2_Install_Unity_2020.1.0b13_%28Beta%29_Panel_with_Microsoft_Visual_Studio_Community_2019_selected.png" alt="02-UnityHub_2.3.2_Install_Unity_2020.1.0b13_(Beta)_Panel_with_Microsoft_Visual_Studio_Community_2019_selected" width="880" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will have to agree to Microsoft Visual Studio Community 2019 License terms. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lgck6oz8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/03-UnityHub_2.3.2_Install_Microsoft_Visual_Studio_Community_2019_Panel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lgck6oz8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/03-UnityHub_2.3.2_Install_Microsoft_Visual_Studio_Community_2019_Panel.png" alt="03-UnityHub_2.3.2_Install_Microsoft_Visual_Studio_Community_2019_Panel" width="880" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, you will see a box with a loading bar meaning that the installation of Unity and Visual Studio is in progress. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EeoTe2J3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/04-UnityHub_2.3.2_Installs_Section_with_Unity_2020.1.0b13_%28Beta%29_being_installed.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EeoTe2J3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://genomagames.com/assets/2020-07-02-how-to-install-unity-2020/04-UnityHub_2.3.2_Installs_Section_with_Unity_2020.1.0b13_%28Beta%29_being_installed.png" alt="04-UnityHub_2.3.2_Installs_Section_with_Unity_2020.1.0b13_(Beta)_being_installed" width="880" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the installation completes, you will be ready to open or start a new project with Unity 2020.&lt;/p&gt;

&lt;p&gt;Checkout &lt;a href="https://docs.unity3d.com/2020.1/Documentation/Manual/GettingStartedInstallingUnity.html"&gt;Unity 2020.1 Manual "Installing Unity" Chapter&lt;/a&gt; for more info&lt;/p&gt;

&lt;p&gt;Happy Game Dev! 👾&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>gamedev</category>
    </item>
  </channel>
</rss>
