<?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: Atena Razm</title>
    <description>The latest articles on DEV Community by Atena Razm (@atenajoon).</description>
    <link>https://dev.to/atenajoon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F685750%2Fd8e16a98-9303-4662-9ff6-7c432c693719.jpeg</url>
      <title>DEV Community: Atena Razm</title>
      <link>https://dev.to/atenajoon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atenajoon"/>
    <language>en</language>
    <item>
      <title>React State Management: When &amp; Where add your states?</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Sun, 30 Jun 2024 19:50:55 +0000</pubDate>
      <link>https://dev.to/atenajoon/react-state-management-when-where-add-your-states-3g61</link>
      <guid>https://dev.to/atenajoon/react-state-management-when-where-add-your-states-3g61</guid>
      <description>&lt;p&gt;When you start learning React, &lt;strong&gt;managing state&lt;/strong&gt; can be challenging at first. It's crucial to understand when you really need a state for a variable and where to place that state to ensure your code is robust and efficient. Proper state management not only &lt;strong&gt;optimizes performance&lt;/strong&gt; by minimizing unnecessary re-renders but also enhances &lt;strong&gt;predictability&lt;/strong&gt; and &lt;strong&gt;maintainability&lt;/strong&gt;, making your code easier to debug. It promotes component &lt;strong&gt;reusability&lt;/strong&gt;, supports application &lt;strong&gt;scalability&lt;/strong&gt;, and maintains a clear &lt;strong&gt;separation of concerns&lt;/strong&gt;. Ultimately, effective state management leads to a smoother user experience and a high-quality, performant application.&lt;/p&gt;

&lt;p&gt;There is a series of questions you can ask yourself to determine if your variables need separate states or if they can be simple constants. Additionally, these questions can help you decide where to keep the state if it is needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  When do I need a state variable?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Do you need to store data?&lt;/strong&gt;&lt;br&gt;
YES:&lt;br&gt;
&lt;strong&gt;Will data change at some point?&lt;/strong&gt;&lt;br&gt;
NO: Regular "const" variable&lt;br&gt;
YES: &lt;br&gt;
&lt;strong&gt;Can it be computed from existing state/props?&lt;/strong&gt;&lt;br&gt;
YES: Derive state&lt;br&gt;
NO: &lt;br&gt;
&lt;strong&gt;Should it re-render the component?&lt;/strong&gt;&lt;br&gt;
NO: Ref(useRef)&lt;br&gt;
YES: &lt;br&gt;
&lt;strong&gt;PLACE A NEW PIECE OF STATE IN COMPONENT!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to place your new state?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Always start with a local state in the current component. Then ask yourself if it's:&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Only used by this component?&lt;/strong&gt;&lt;br&gt;
YES: Leave it in the component&lt;br&gt;
NO: &lt;strong&gt;Also used by a child component?&lt;/strong&gt;&lt;br&gt;
YES: pass it to the child via props&lt;br&gt;
NO: &lt;strong&gt;Also used by one or a few sibling components?&lt;/strong&gt;&lt;br&gt;
YES: List state up to first common parent&lt;br&gt;
NO: &lt;strong&gt;Used all over the component tree by more than a few sibling components?&lt;/strong&gt;&lt;br&gt;
YES: Then, you probably need a Global State!&lt;/p&gt;

</description>
      <category>react</category>
      <category>state</category>
      <category>useref</category>
    </item>
    <item>
      <title>A practical summary of React State variables &amp; Props!</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Fri, 21 Jun 2024 20:49:37 +0000</pubDate>
      <link>https://dev.to/atenajoon/react-state-variables-vs-props-21o7</link>
      <guid>https://dev.to/atenajoon/react-state-variables-vs-props-21o7</guid>
      <description>&lt;p&gt;There are so many videos and documentation out there to help you learn the concept and usage of &lt;em&gt;State&lt;/em&gt; and &lt;em&gt;Props&lt;/em&gt; with code examples. this article is not about teaching you these concepts from scratch or providing you with code examples. The main point of this blog is to briefly give you a practical summary of &lt;strong&gt;what they are&lt;/strong&gt; and &lt;strong&gt;when to use them&lt;/strong&gt;. So, if you are completely new to these concepts, I would suggest you first watch some basic tutorials about them to get the most out of this summary.&lt;/p&gt;

&lt;h2&gt;
  
  
  State
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is State?
&lt;/h3&gt;

&lt;p&gt;The state variable is a plain JavaScript object used by React that includes all information about the component's current situation. State variables are immutable and private to that component.&lt;/p&gt;

&lt;h3&gt;
  
  
  When do we need a state for a component?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Whenever you need to change/update part of the UI&lt;/li&gt;
&lt;li&gt;Whenever the user is supposed to interact with the UI&lt;/li&gt;
&lt;li&gt;Whenever you need to persist local variables between renders&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using state correctly:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;States are immutable. The component that owns a piece of the state, should be the one that modifies it. Do not modify the state directly. Instead, use &lt;strong&gt;setState()&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;State updates may be asynchronous. So, if you need to use the value of &lt;em&gt;this.props&lt;/em&gt; and &lt;em&gt;this.state&lt;/em&gt; to calculate the next state value, instead of directly passing an object to &lt;em&gt;setState()&lt;/em&gt; pass a callback function with the &lt;em&gt;state&lt;/em&gt; and &lt;em&gt;props&lt;/em&gt; as arguments.&lt;/li&gt;
&lt;li&gt;State updates are merged.&lt;/li&gt;
&lt;li&gt;The data flows down.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How does setState() work?
&lt;/h3&gt;

&lt;p&gt;This method tells React that we are updating the state. Then, React checks which parts of the state are changed to bring the DOM in sync with the virtual DOM based on those changes.&lt;/p&gt;

&lt;p&gt;Note: &lt;strong&gt;props&lt;/strong&gt; is basically a plain JavaScript object that includes all the attributes that we set in the parent component. Every React component has the &lt;em&gt;props property&lt;/em&gt;. It is used to pass data from a parent component to a child. It is &lt;em&gt;“read-only”&lt;/em&gt; and cannot be modified, if we need to do so, we should use setState and modify them from the state.&lt;/p&gt;

&lt;h2&gt;
  
  
  State vs props
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Ownership and Control:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;State:&lt;/strong&gt; Internal data, owned and controlled by the component itself.&lt;br&gt;
&lt;strong&gt;Props:&lt;/strong&gt; External data, owned and controlled by the parent component.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conceptual Understanding:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;State:&lt;/strong&gt; Can be thought of as the component's memory, holding data over time across multiple re-renders.&lt;br&gt;
&lt;strong&gt;Props:&lt;/strong&gt; Like function parameters, used to pass data from parent to child components.&lt;/p&gt;

&lt;h4&gt;
  
  
  Mutability and Effects:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;State:&lt;/strong&gt; Can be updated by the component itself, triggering a re-render.&lt;br&gt;
&lt;strong&gt;Props:&lt;/strong&gt; Read-only, cannot be modified by the receiving component. Receiving new props also causes a re-render.&lt;/p&gt;

&lt;h4&gt;
  
  
  Usage:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;State:&lt;/strong&gt; Used to make components interactive.&lt;br&gt;
&lt;strong&gt;Props:&lt;/strong&gt; Used to configure child components from the parent component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Whenever a piece of state is updated, the component that owns the state, and all children components that receive the piece of state as props will be re-rendered to keep in sync. &lt;/p&gt;

</description>
      <category>reactjsdevelopment</category>
      <category>state</category>
      <category>props</category>
      <category>react</category>
    </item>
    <item>
      <title>Short-Circuiting and Logical Operators in JavaScript: &amp;&amp;, ||, ??</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Fri, 07 Jun 2024 21:14:04 +0000</pubDate>
      <link>https://dev.to/atenajoon/short-circuiting-and-logical-operators-in-javascript--13eh</link>
      <guid>https://dev.to/atenajoon/short-circuiting-and-logical-operators-in-javascript--13eh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Short-circuiting&lt;/strong&gt; is a concept in logical operations in many programming languages, including JavaScript where the evaluation stops as soon as the outcome is determined. such as the “AND operator” and “OR operator”. This feature only looks at the first value to decide what to return without even looking at the second value.&lt;br&gt;
It is particularly useful for optimizing code and preventing unnecessary computations.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&amp;amp;&amp;amp; operator&lt;/strong&gt;: if the first value is &lt;strong&gt;true&lt;/strong&gt;, it immediately returns “the second value”, and if the first value is &lt;strong&gt;falsy&lt;/strong&gt; it will immediately return “the falsy value”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// falsy values: 0, '', undefined, null
console.log(true &amp;amp;&amp;amp; 'Something'); // Something
console.log(false &amp;amp;&amp;amp; 'Something'); // false
console.log(0 &amp;amp;&amp;amp; 'Something'); // 0
11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;|| operator&lt;/strong&gt;: if the first value is &lt;strong&gt;true&lt;/strong&gt;, it immediately returns “true”, and if the first value is &lt;strong&gt;falsy&lt;/strong&gt; it will immediately return “the second value”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// falsy values: 0, '', undefined, null
console.log(true || 'Something'); // true
console.log(false || 'Something'); // Something
console.log(0 || 'Something'); // Something
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there can be a problem with the returned value since if the first value is 0 but it’s not actually false, it’s just falsy! Then we would need to get the actual value of 0, not the second value.&lt;br&gt;
Another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const count1 = undefined;
const count2 = 0;
console.log(count1 || 'No data'); // No data
console.log(count2 || 'No data'); // No data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the second console log should show 0 as the total number, not undefined. To fix this, we should give the condition some flexibility with falsy values other than null and undefined. That’s where we use the &lt;strong&gt;Nullish coalescing operator&lt;/strong&gt;. This operator returns the falsy value if it’s anything other than undefined or null. So, for the values of 0 and empty strings, it will return the actual value of them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const count1 = undefined;
const count2 = 0;
const count3 = 5;
console.log(count1 ?? 'No data'); // No data
console.log(count2 ?? 'No data'); // 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Optional chaining operator&lt;/strong&gt;: this can be very useful when dealing with objects that may have missing properties or when you want to safely access deeply nested properties without running into &lt;em&gt;null&lt;/em&gt; or &lt;em&gt;undefined&lt;/em&gt; errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const data = {
 value1: undefined, // or it could be { nested: undefined }
 value2: { nested: 3 },
 value3: { nested: 5 }
};
function getMultiplicationResult(obj1, obj2) {
 const val1 = obj1?.nested ?? 0;
 const val2 = obj2?.nested ?? 0;
 return val1 * val2;
}
const result = getMultiplicationResult(data.value1, data.value2); // 0 * 3 = 0
const result = getMultiplicationResult(data.value2, data.value3); // 3 * 5 = 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we used &lt;strong&gt;?&lt;/strong&gt; (Optional Chaining) to safely access the &lt;em&gt;nested&lt;/em&gt; property of the objects &lt;em&gt;obj1&lt;/em&gt; and &lt;em&gt;obj2&lt;/em&gt;.&lt;br&gt;
And used &lt;strong&gt;??&lt;/strong&gt; (Nullish Coalescing) to provide a default value of &lt;em&gt;0&lt;/em&gt; if &lt;em&gt;nested&lt;/em&gt; is &lt;em&gt;null&lt;/em&gt; or &lt;em&gt;undefined&lt;/em&gt;.&lt;br&gt;
So, if &lt;em&gt;data.value1&lt;/em&gt; is undefined or if &lt;em&gt;data.value1.nested&lt;/em&gt; is undefined, &lt;em&gt;val1&lt;/em&gt; will be set to &lt;em&gt;0&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Async/Await keeps order in JavaScript;</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Thu, 06 Jun 2024 19:41:57 +0000</pubDate>
      <link>https://dev.to/atenajoon/asyncawait-keeps-order-in-javascript-2k4p</link>
      <guid>https://dev.to/atenajoon/asyncawait-keeps-order-in-javascript-2k4p</guid>
      <description>&lt;p&gt;JavaScript is a single-threaded synchronous language, meaning it goes through the statements in the order they are written and processes one at a time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("cook!");
console.log("eat!");
console.log("clean!");

// cook!
// eat!
// clean!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If before the &lt;em&gt;cook!&lt;/em&gt; operation there was an asynchronous operation like fetching the recipe from an API, the program might not wait for it to complete before moving on to the &lt;em&gt;cook!&lt;/em&gt;, &lt;em&gt;eat!&lt;/em&gt;, and &lt;em&gt;clean!&lt;/em&gt; operations. This can result in out-of-order execution, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetchRecipeFromAPI(); // Assume this is an asynchronous function
console.log("cook!");
console.log("eat!");
console.log("clean!");

// Output might be:
// cook!
// eat!
// clean!
// (sometime later) Recipe fetched!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, we definitely need to get the recipe first, and then cook based on it!&lt;br&gt;
To ensure the steps are executed in order, even if one of them is asynchronous, we can use &lt;strong&gt;async&lt;/strong&gt; and &lt;strong&gt;await&lt;/strong&gt; keywords. These little friends can help us write asynchronous code that appears synchronous, making it easier to understand and maintain.&lt;/p&gt;

&lt;p&gt;There are 2 steps to use them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define the asynchronous function with the &lt;strong&gt;async&lt;/strong&gt; keyword before it.&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;await&lt;/strong&gt; keyword inside your asynchronous function to pause the execution until the operation is completed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the following example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;fetchRecipeFromAPI&lt;/strong&gt; is an asynchronous function simulating a recipe fetch operation with a delay.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;cook&lt;/strong&gt; is an asynchronous function that waits for fetchRecipeFromAPI to complete before continuing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;main&lt;/strong&gt; is an asynchronous function that ensures cook, eat!, and clean! happen in order.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchRecipeFromAPI() {
    return new Promise((resolve) =&amp;gt; {
        setTimeout(() =&amp;gt; {
            console.log("Recipe fetched!");
            resolve("recipe");
        }, 2000); // Simulate an API call with a 2-second delay
    });
}

async function cook(recipe) {
    console.log("start cooking with", recipe);
}

async function main() {
    const recipe = await fetchRecipeFromAPI(); // Wait for the recipe to be fetched
    await cook(recipe);  // Wait for the cooking to complete
    console.log("eat!");
    console.log("clean!");
}

main();

// Output:
// Recipe fetched!
// cook!
// start cooking with recipe
// eat!
// clean!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;strong&gt;async&lt;/strong&gt; and &lt;strong&gt;await&lt;/strong&gt;, you ensure that the asynchronous &lt;em&gt;cook&lt;/em&gt; function completes before moving on to &lt;em&gt;eat!&lt;/em&gt; and &lt;em&gt;clean!&lt;/em&gt;. This maintains the logical sequence of operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you try to get the value of an asynchronous function outside of it, It's always gonna be &lt;em&gt;Promise {  }&lt;/em&gt;, because JavaScript has no way of knowing what the result will be outside of that asynchronous process.&lt;/p&gt;

&lt;p&gt;You can find the actual code in this &lt;a href="https://codesandbox.io/p/sandbox/get-advice-app-react-7d2k9f?file=%2Fsrc%2FTest.js"&gt;SandBox&lt;/a&gt;&lt;/p&gt;

</description>
      <category>asynchronous</category>
      <category>javascript</category>
      <category>api</category>
      <category>react</category>
    </item>
    <item>
      <title>Essential Summary of Unity's New Input System</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Wed, 17 Jan 2024 05:31:00 +0000</pubDate>
      <link>https://dev.to/atenajoon/essential-summary-of-unitys-new-input-system-2ee8</link>
      <guid>https://dev.to/atenajoon/essential-summary-of-unitys-new-input-system-2ee8</guid>
      <description>&lt;h2&gt;
  
  
  The Input Action System
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The basic idea
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The User presses a button on a device 
(User &amp;gt; Device &amp;gt; Interaction)&lt;/li&gt;
&lt;li&gt;There is a Binding between Device, Interaction, and Actions&lt;/li&gt;
&lt;li&gt;The Action calls a method in our code&lt;/li&gt;
&lt;li&gt;The code reacts to the interaction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8wo2b5fpyiz4smmb6yz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo8wo2b5fpyiz4smmb6yz.png" alt="Unity Input System Response Steps in a picture" width="800" height="200"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/manual/Concepts.html"&gt;Photo's reference&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Input Action Maps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An Action Map provides controls for different circumstances and levels.&lt;/li&gt;
&lt;li&gt;Each Action Map has a name and an ID too, which are unique within the Action Asset that the Action Map belongs to (if any).&lt;/li&gt;
&lt;li&gt;For an Action to do something, you must first enable it, individually or in bulk through Action maps. When you enable an Action, the Input System resolves its bindings.&lt;/li&gt;
&lt;li&gt;To stop Actions or Action Maps from responding to input, call Disable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what happens on Enabling an action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Update()/FixedUpdate()/ manual updates 
{
  &amp;gt; The Action actively monitors the Control(s) it's bound to
  &amp;gt; If (a bound Control state changes)
    {
       The action processes the change 
       {
          &amp;gt; If (the Control's change represents an 
                 Interaction change)
            {
               &amp;gt; The action creates a response.
            }
        }
     }
 }

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Each Action Map has an InputActionMap.&lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Actions.html#inputactionmapactiontriggered-callback:~:text=InputActionMap.actionTriggered%20callback"&gt;actionTriggered &lt;/a&gt;callback.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Input Actions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Input Actions are designed to separate the logical meaning of an input from the physical means of input.&lt;/li&gt;
&lt;li&gt;Actions use InputBinding to refer to the inputs they collect.&lt;/li&gt;
&lt;li&gt;Each Action has a name (InputAction.name), which must be unique within the Action Map that the Action belongs to, and a unique ID (InputAction.id), which you can use to reference the Action.&lt;/li&gt;
&lt;li&gt;The Actions can be: created in its dedicated ActionAssets’ Editor, embedded in the MoboBehaviour components, manually loaded from a JSON file, or created directly in code.
**Note: **You must manually enable and disable Actions and Action Maps that are embedded in MonoBehaviour components.&lt;/li&gt;
&lt;li&gt;An Action doesn't represent an actual response to input by itself. It informs your code that a certain type of input has occurred. Your code then responds to this.&lt;/li&gt;
&lt;li&gt;Each Action has a started, performed, and canceled &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Actions.html#started-performed-and-canceled-callbacks"&gt;callback&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You can poll the current state of an Action (the current value of an action) whenever you need it; to do this, use &lt;strong&gt;InputAction.ReadValue&amp;lt;&amp;gt;()&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;InputActionTrace&lt;/strong&gt; can record changes happening on Actions.&lt;/li&gt;
&lt;li&gt;You can also use &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Components.html#notification-behaviors"&gt;PlayerInput&lt;/a&gt; to pick up input from Actions.&lt;/li&gt;
&lt;li&gt;Action phases in receiving an input: Disabled, Waiting, Started, Performed, Canceled. &lt;/li&gt;
&lt;li&gt;You can read the current phase of an action using &lt;strong&gt;InputAction.phase&lt;/strong&gt;. The last 3 phases have a callback associated with them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Each callback receives an &lt;strong&gt;InputAction.CallbackContext&lt;/strong&gt; structure, which holds context information that you can use to query the current state of the Action and to read out values from Controls that triggered the Action (InputAction.CallbackContext.ReadValue).&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; The contents of the structure are only valid for the duration of the callback. In particular, it isn't safe to store the received context and later access its properties from outside the callback.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When and how the callbacks are triggered depends on the Interactions present on the respective Bindings. If the Bindings have no Interactions that apply to them, the default Interaction applies.&lt;/li&gt;
&lt;li&gt;At runtime &amp;gt;&amp;gt; a Controller sends an Input &amp;gt;&amp;gt; an Interaction completes &amp;gt;&amp;gt; the Action triggered &amp;gt;&amp;gt; the code responses&lt;/li&gt;
&lt;li&gt;There are 3 different &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.InputActionType.html"&gt;Action Types&lt;/a&gt; that you can select in the Input Action editor window, or by specifying it as a parameter when you call the InputAction() constructor. The default Action Type is Value.
Learn More about them &lt;a href="https://www.youtube.com/watch?v=DMUZfVSYJfs"&gt;here&lt;/a&gt;.
In Summary:&lt;/li&gt;
&lt;li&gt;Use the &lt;strong&gt;Value type&lt;/strong&gt; for any inputs that should track continuous changes to the state of a Control. When the Action is initially enabled, it performs an initial state check of all bound Controls. If any of them is actuated, the Action then triggers a callback with the current value.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Button type&lt;/strong&gt; is very similar to the Value type, but its Actions can only be bound to ButtonControl controls, and it doesn’t perform the initial state check as the Value one does. Use this for inputs that trigger an Action once every time they are pressed.&lt;/li&gt;
&lt;li&gt;With the &lt;strong&gt;Pass-Through type&lt;/strong&gt;, any change to any bound Control triggers a callback with that Control's value. It won’t try to determine where this value is coming from. This is useful if you just have one set of controls, and want to process all input from a set of Controls.&lt;/li&gt;
&lt;li&gt;To see currently enabled Actions and their bound Controls, use the &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Debugging.html#debugging-actions"&gt;Input Debugger&lt;/a&gt;. Here is a &lt;a href="https://www.google.com/search?q=unity+inputaction+visualizer&amp;amp;source=lmns&amp;amp;tbm=vid&amp;amp;bih=685&amp;amp;biw=1309&amp;amp;rlz=1C5CHFA_enCA968CA968&amp;amp;hl=en&amp;amp;sa=X&amp;amp;ved=2ahUKEwiho_epgcWDAxX7ETQIHUa2A50Q0pQJKAF6BAgBEAQ#fpstate=ive&amp;amp;vld=cid:1bf2d7e1,vid:ICh1ZEaVUjc,st:0"&gt;tutorial&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You can also use the &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Debugging.html#inputactionvisualizer"&gt;InputActionVisualizer&lt;/a&gt; component from the Visualizers sample to get an on-screen visualization of an Action's value and Interaction state in real-time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Input Action Asset
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/ActionAssets.html"&gt;Input Action Asset&lt;/a&gt; is an Asset that contains Input Actions and their associated Bindings and Control Schemes. These Assets have the &lt;em&gt;.inputactions&lt;/em&gt; file extension and are stored in a plain JSON format.&lt;/li&gt;
&lt;li&gt;In the Control picker window, you can explore a tree of Input Devices and Controls that the Input System recognizes and binds to these Controls. &lt;/li&gt;
&lt;li&gt;The Device and Control tree is organized hierarchically from generic to specific. &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/ActionAssets.html#:~:text=The%20Device%20and%20Control%20tree%20is%20organized%20hierarchically%20from%20generic%20to%20specific."&gt;Ref&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Instead of browsing the tree to find the Control you want, it's easier to let the Input System listen for input. &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/ActionAssets.html#:~:text=Instead%20of%20browsing%20the%20tree%20to%20find%20the%20Control%20you%20want%2C%20it%27s%20easier%20to%20let%20the%20Input%20System%20listen%20for%20input."&gt;Ref&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Finally, you can choose to manually edit the Binding path, instead of using the Control picker. &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/ActionAssets.html#:~:text=Finally%2C%20you%20can%20choose%20to%20manually%20edit%20the%20Binding%20path%2C%20instead%20of%20using%20the%20Control%20picker."&gt;Ref&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Using Input Action Assets
&lt;/h4&gt;

&lt;p&gt;*&lt;em&gt;Auto-generating script code for Actions - *&lt;/em&gt; &lt;a href="https://youtu.be/m5WsmlEOFiA?t=609"&gt;Video&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One of the most convenient ways to work with .inputactions Assets in scripts is to automatically generate a C# wrapper class for them. 
&lt;em&gt;Using Action Assets with PlayerInput Component&lt;/em&gt; - &lt;a href="https://youtu.be/m5WsmlEOFiA?t=709"&gt;Video&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;set up all your Actions in an Input Action Asset
&amp;gt;&amp;gt; Then assign it to the &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Components.html#playerinput-component"&gt;PlayerInput component&lt;/a&gt;
&amp;gt;&amp;gt; which can automatically handle activating Action Maps and selecting Control Schemes for you&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Input Bindings
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;During the &lt;strong&gt;disambiguation&lt;/strong&gt; process, the Input System looks at the value of each Control bound to an Action. If the magnitude of the input from any Control is higher than the &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Controls.html#control-actuation:~:text=It%20can%20be%20useful%20to%20determine%20not%20just%20whether%20a%20Control%20is%20actuated%20at%20all%2C%20but%20also%20the%20amount%20by%20which%20it%20is%20actuated%20(that%20is%2C%20its%20magnitude).%20For%20example%2C%20for%20a%20Vector2Control%20this%20would%20be%20the%20length%20of%20the%20vector%2C%20whereas%20for%20a%20button%20it%20is%20the%20raw%2C%20absolute%20floating%2Dpoint%20value."&gt;magnitude&lt;/a&gt; of the Control currently driving the Action, then the Control with the higher magnitude becomes the new Control driving the Action.&lt;/li&gt;
&lt;li&gt;If you don't want your Action to perform disambiguation, you can set your Action type to Pass-Through.&lt;/li&gt;
&lt;li&gt;Runtime rebinding allows users of your application to set their own Bindings.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Input Interactions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Interactions.html"&gt;Interaction&lt;/a&gt; represents a specific input pattern. For example, a hold is an Interaction that requires a Control to be held for at least a minimum amount of time.&lt;/li&gt;
&lt;li&gt;Interaction’s phases: Waiting, Starting, Performed, Canceled&lt;/li&gt;
&lt;li&gt;Not every Interaction triggers every phase.&lt;/li&gt;
&lt;li&gt;Using Interactions: You can install Interactions on Bindings or Actions.&lt;/li&gt;
&lt;li&gt;Interactions on Actions work very similar to Interactions on Bindings, but they affect all Controls bound to an Action, not just the ones coming from a specific Binding. If there are Interactions on both the Binding and the Action, the Input System processes the ones from the binding first.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are the &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Interactions.html#:~:text=duration%3D0.8)%22)%3B-,Predefined%20Interactions,-The%20Input%20System"&gt;Predefined Interactions&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Default Interaction&lt;br&gt;
Press&lt;br&gt;
Hold&lt;br&gt;
Tap&lt;br&gt;
SlowTap&lt;br&gt;
MultiTap&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can write your own &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Interactions.html#:~:text=were%20too%20long).-,Writing%20custom%20Interactions,-You%20can%20also"&gt;custom Interaction&lt;/a&gt; and use  it in the UI and code the same way you use built-in Interactions. To do so, add a class implementing the &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.IInputInteraction.html"&gt;IInputInteraction&lt;/a&gt; interface.&lt;/p&gt;

&lt;h3&gt;
  
  
  Processor
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It will process your Input after it’s received. For example, it can Normalize the value of the input you receive if you only care about the direction of the input to move your character, and not the Value of it.&lt;/li&gt;
&lt;li&gt;Learn about the Deadzone processor type &lt;a href="https://youtu.be/17VqOh3_zxU?t=188"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You can choose multiple Processors, and it depends on the type of your Action.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Character's Movement handling in Unity Input System</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Wed, 17 Jan 2024 05:30:34 +0000</pubDate>
      <link>https://dev.to/atenajoon/characters-movement-handling-in-unity-input-system-49ck</link>
      <guid>https://dev.to/atenajoon/characters-movement-handling-in-unity-input-system-49ck</guid>
      <description>&lt;p&gt;To handle the movement of a game object I have 2 ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using Rigidbody Physics&lt;/li&gt;
&lt;li&gt;Using Transform for Direct Manipulation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's check each of them separately.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Using Rigidbody Physics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pros:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Realistic physics interactions with the environment using Unity’s physics engine.&lt;/li&gt;
&lt;li&gt;Built-in features like gravity, collision detection, and mass which can simplify the implementation of certain behaviors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cons:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It might add extra complexity and Tuning to get the movement exactly how you want it, for example in the jumping or fraction scenarios.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach is usually recommended for a more physics-based or complex 2D game.&lt;br&gt;
There are different ways to control the movement of a Rigidbody:&lt;/p&gt;
&lt;h3&gt;
  
  
  A. Rigidbody.AddForce
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Used to apply a force to the Rigidbody.&lt;br&gt;
&lt;strong&gt;Behavior:&lt;/strong&gt; Adds a force to the object, taking into account its mass, resulting in acceleration. Continuously applying force can lead to continuous acceleration.&lt;br&gt;
&lt;strong&gt;Use Case:&lt;/strong&gt; Suitable for simulating realistic physics-based movements, such as jumping, pushing objects, or responding to external forces.&lt;/p&gt;

&lt;p&gt;Use it when you want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to simulate forces and acceleration in a more physics-based manner.&lt;/li&gt;
&lt;li&gt;realistic interactions with other objects, gravity, and forces are desired.&lt;/li&gt;
&lt;li&gt;to handle scenarios like jumping, falling, or being pushed by external forces.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; float movementInput = playerActionControls.Land.Move.ReadValue&amp;lt;float&amp;gt;();
 float horizontalForce = movementInput * _moveSpeed;
 _rigidbody.AddForce(new Vector2(horizontalForce, 0f));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  B. Rigidbody.velocity
&lt;/h3&gt;

&lt;p&gt;v: Velocity vector&lt;br&gt;
Δd: Change in position vector&lt;br&gt;
Δt: Change in time&lt;/p&gt;

&lt;p&gt;Here is its formula: Velocity(v)= Δt / Δd&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Purpose:&lt;/strong&gt; Used to directly set the velocity of the Rigidbody.&lt;br&gt;
&lt;strong&gt;Behavior:&lt;/strong&gt; Sets the object's velocity directly, ignoring mass. The object will move at a constant speed in the specified direction, without continuous acceleration unless additional forces are applied.&lt;br&gt;
&lt;strong&gt;Use Case:&lt;/strong&gt; Suitable for situations where you want direct control over the object's speed and direction without simulating realistic physics-based forces.&lt;/p&gt;

&lt;p&gt;Use it when you want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;direct control over the object's speed and direction without simulating complex physics forces.&lt;/li&gt;
&lt;li&gt;the object to move at a constant speed without acceleration.&lt;/li&gt;
&lt;li&gt;to create more straightforward, predictable movements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; float movementInput = playerActionControls.Land.Move.ReadValue&amp;lt;float&amp;gt;();
 float horizontalMovement = movementInput * _moveSpeed * Time.deltaTime;  
 _rigidbody.velocity = new Vector2(horizontalMovement, _rigidbody.velocity.y);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In this example, &lt;em&gt;Time.deltaTime&lt;/em&gt; is used to ensure that the movement is frame-rate independent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In Unity, when using Rigidbody to handle physics-based movement, it's generally recommended to use either &lt;strong&gt;Rigidbody.AddForce&lt;/strong&gt; or &lt;strong&gt;Rigidbody.velocity&lt;/strong&gt; but not both in the same frame. Mixing them can lead to unexpected behavior because they represent different ways of applying forces to an object.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Using Transform for Direct Manipulation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pros:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Simplicity of implementing the basic movements.&lt;/li&gt;
&lt;li&gt;Predictability of the behavior by having more control over the character’s position.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Cons:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Limited physics interactions&lt;/li&gt;
&lt;li&gt;Manual handling for features like jumping, ground detection, and responding to collisions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Using &lt;strong&gt;transform.Translate&lt;/strong&gt; for movement is generally better suited for non-physics-based scenarios or when you want to handle movement manually without relying on the physics engine. In such cases, make sure to disable the Rigidbody's gravity and kinematic properties.&lt;/p&gt;

&lt;p&gt;This approach is usually recommended for simple 2D platformers with basic movements.&lt;br&gt;
Code Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;transform.Translate(new Vector3(horizontalMovement, 0, 0));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Using Unity New Input System in my Shooting Platformer</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Wed, 17 Jan 2024 05:26:39 +0000</pubDate>
      <link>https://dev.to/atenajoon/using-unity-new-input-system-in-my-shooting-platformer-43p2</link>
      <guid>https://dev.to/atenajoon/using-unity-new-input-system-in-my-shooting-platformer-43p2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Unity's new Input System is a powerful and flexible system designed to simplify the process of creating, modifying, and reusing input configurations across different platforms and devices. &lt;/p&gt;

&lt;p&gt;Before using the new Input System, we have a few interesting &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/manual/Concepts.html"&gt;concepts&lt;/a&gt; like User, Device &amp;amp; Controls, Interaction, Actions, Action Map, Processor, and Workflows to learn and make our lives easier. Need a fast-forward? Check out my "&lt;a href="https://dev.to/atenajoon/essential-summary-of-unitys-new-input-system-2ee8"&gt;Essential Summary of Unity's New Input System&lt;/a&gt;" blog then!&lt;/p&gt;

&lt;h2&gt;
  
  
  My Code Walk-through
&lt;/h2&gt;

&lt;p&gt;I am about to explain my steps for implementing the new Input System to &lt;a href="https://atenajoon.itch.io/2dshoot"&gt;my 2D Shooter game&lt;/a&gt; character to control its horizontal movement as well as jumps.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Setting the Actions in the ActionMap
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Right-clicked in the &lt;em&gt;Projects&lt;/em&gt; tab&lt;/li&gt;
&lt;li&gt;Selected the &lt;em&gt;Input Actions&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Opened the newly created Input Action in its Editor window&lt;/li&gt;
&lt;li&gt;Made the &lt;em&gt;Land&lt;/em&gt; Action Map and 3 Actions in it: Move, Jump, Fire&lt;/li&gt;
&lt;/ol&gt;

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

&lt;h3&gt;
  
  
  Move
&lt;/h3&gt;

&lt;p&gt;I set the Action Type to “Value”, because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It does disambiguation (finds the active control device)&lt;/li&gt;
&lt;li&gt;It automatically sends out the Start and Canceled callbacks as well as Performed. (Moving the joystick sends out the started and performed callbacks, and as soon as the action value is back to the initial one, the canceled callback cancels the action)&lt;/li&gt;
&lt;li&gt;It’s the only action type that does the initial state check&lt;/li&gt;
&lt;li&gt;It’s good for one-player&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Jump
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Set the Action Type to Button&lt;/li&gt;
&lt;li&gt;Checked the “Initial State Check” checkbox, &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The jump only happens once every time the key is pressed&lt;/li&gt;
&lt;li&gt;It jumps again only if the key is up and pressed again&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Fire
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Set it to Button Type&lt;/li&gt;
&lt;li&gt;I didn’t check the checkbox&lt;/li&gt;
&lt;li&gt;I set the Interaction to “Press” &amp;gt;&amp;gt; “Press Only”,&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firing is triggered with a button&lt;/li&gt;
&lt;li&gt;The button type only has the “Performed” phase&lt;/li&gt;
&lt;li&gt;and, I wanted to customize the key's behavior to instantiate Bullets as long as the Key was pressed down, not only once on each press&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; There are no Started or Canceled phases for the Button type. However, it has two Performed callbacks, one at pressing the button down and one at the release, which I used in my OnFire() code to simulate Started and Canceled callbacks.  Learn more about Input System Action Types &lt;a href="https://youtu.be/DMUZfVSYJfs?t=219"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Created new Bindings
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Set up the Composite Bindings
&lt;/h3&gt;

&lt;p&gt;Move &amp;gt;&amp;gt; Type: 1D Axis&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcquebraioubtc5bwy95j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcquebraioubtc5bwy95j.png" alt="Input Action Editor Window" width="800" height="561"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; 1D Axis type…&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;represents a one-dimensional input axis, &lt;/li&gt;
&lt;li&gt;is typically used for joystick axes, triggers on game controllers, or scroll wheels on a mouse, &lt;/li&gt;
&lt;li&gt;has a range of values from -1.0 to 1.0,&lt;/li&gt;
&lt;li&gt;when you read the value of a 1D Axis in your code, you'll typically get a float value representing the current position of the input along that axis.
Example:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;float horizontalInput = playerActionControls.Player.Move.ReadValue&amp;lt;float&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Jump and Fire &amp;gt;&amp;gt; There is no Composite for the Button type. Just set the Binding path to the desired key by using the “Listen” button:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl3vwil6pn97grnubuf34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl3vwil6pn97grnubuf34.png" alt="Input Action Editor Window" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Generated C# class file for it
&lt;/h2&gt;

&lt;p&gt;I chose the InputAction and in the Inspector checked the “Generate C# Class”, named the C# file (PlayerActionControls), and pressed &lt;em&gt;Apply&lt;/em&gt;. &lt;a href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.7/manual/Workflow-ActionsAsset.html#:~:text=bad%20for%20performance.-,Referencing%20the%20Actions%20Asset%20through%20a%20C%23%20wrapper,-To%20use%20your"&gt;Ref&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdkfp7zfhyefbvicx7m7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkdkfp7zfhyefbvicx7m7.png" alt="PlayerActionControls in the Inspector Window" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Working with the Input values
&lt;/h2&gt;

&lt;p&gt;Then, I created another script called &lt;strong&gt;PlayerController&lt;/strong&gt; to instantiate the input controller and work with the input values.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Scripting the Character’s Movement
&lt;/h2&gt;

&lt;p&gt;Inside the PlayerController script:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I instantiated the actions wrapper class inside the &lt;strong&gt;Awake()&lt;/strong&gt; method.&lt;/li&gt;
&lt;li&gt;Enabled and disabled the action wrapper class&lt;/li&gt;
&lt;li&gt;Added callback methods for jump and fire's "Performed" phases.&lt;/li&gt;
&lt;li&gt;Called OnMove() method inside Update()
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void Awake()
{
   // Instantiate the actions wrapper class
   playerActionControls = new PlayerActionControls();
   _rigidbody = GetComponent&amp;lt;Rigidbody2D&amp;gt;();

   // For the jump &amp;amp; fire actions, adding a callback method for the "performed" phase
   playerActionControls.Land.Jump.performed += OnJump;

   playerActionControls.Land.Fire.performed += OnFire;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; 2 ways to check the input system is working:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// float jumpInput = playerActionControls.Land.Jump.ReadValue&amp;lt;float&amp;gt;();
       // if (jumpInput == 1)
       if(playerActionControls.Land.Jump.triggered)
       Debug.Log("jump");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Character's Movement
&lt;/h3&gt;

&lt;p&gt;In the &lt;strong&gt;OnMove()&lt;/strong&gt; method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I read the movement input value from the PlayerActionControls, each frame,&lt;/li&gt;
&lt;li&gt;I stored this value in a private field called _&lt;em&gt;movementInput&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;then multiplied it to the _&lt;em&gt;moveSpeed&lt;/em&gt; Speed SerializeField to control the movement speed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Read the movement input value each frame
float _movementInput = playerActionControls.Land.Move.ReadValue&amp;lt;float&amp;gt;();

// Horizontal movement of the player character
float horizontalMovement = _movementInput * _moveSpeed;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, I tried different ways to handle the movement of the character:&lt;br&gt;
At first, I used the transform method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To achieve continuous movement while holding down the arrow key, I needed to multiply the input value by the movement speed and then multiply it by Time.deltaTime.&lt;/li&gt;
&lt;li&gt;I also used the “Translate” method for smoother movement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But then, I realized that I would have some issues with rotating the _&lt;em&gt;firePoint&lt;/em&gt; this way, so I replaced it with the Rigidbody.velocity() method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I didn’t need the Time.deltaTime multiplication anymore. The physics system took care of everything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read more about the options for handling the character's movements using &lt;strong&gt;Rigidbody Physics&lt;/strong&gt; in my "&lt;a href="https://dev.to/atenajoon/characters-movement-handling-in-unity-input-system-49ck"&gt;Character's Movement handling in Unity Input System&lt;/a&gt;" blog.&lt;/p&gt;
&lt;h3&gt;
  
  
  Character's Flip
&lt;/h3&gt;

&lt;p&gt;To rotate the character UI toward the movement direction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I tried using &lt;em&gt;transform.Rotate(x, y, z);&lt;/em&gt; and changed the y to 180, but then I had problems with spawning the bullets towards the _firePoint’s rotation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you noticed, although this is a 2D game, we still need to set the z-axis value to 0f here, because in fact there is no real 2D in Unity, it’s just an orthographic projection, which is a form of parallel projection in which the top, front, and side of an object are projected onto a perpendicular plane.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So, I just flipped the character inside the OnMove() method, by changing the localScale.x on the Flip method. I flipped the _firePoint direction the same way.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  OnMove()
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // Flip the character sprite to the move direction
  if (_movementInput &amp;gt; 0 &amp;amp;&amp;amp; !_playerFacingRight)
  {
    Flip();
  }
  else if(_movementInput &amp;lt; 0 &amp;amp;&amp;amp; _playerFacingRight)
  {
     Flip();
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Flip()
&lt;/h4&gt;


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

// Flip the character by changing localScale.x
Vector3 newScale = transform.localScale;
newScale.x *= -1;
transform.localScale = newScale;

// Flip the FirePoint direction
Vector3 firePointScale = _firePoint.localScale;
firePointScale.x *= -1;
_firePoint.localScale = firePointScale;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;I also used Quaternion.Euler() in the Fire() method to flip the bullet spawns towards the direction of the Player character. You will see the code later in the &lt;strong&gt;Fire&lt;/strong&gt; section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; A quaternion is a mathematical representation of rotations in 3-dimensional space. Eular angles represent rotations as three separate angles along the coordinate axis (x, y, z). &lt;strong&gt;Quaternion.Euler&lt;/strong&gt; is a Unity method that takes 3 float parameters representing the rotation angles and creates a rotation quaternion based on Euler angles.&lt;/p&gt;
&lt;h3&gt;
  
  
  Character's Jump
&lt;/h3&gt;

&lt;p&gt;For the Jumping action:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I added the Rigidbody component and checked the Freeze Rotation Z checkbox to prevent the character from rotating and falling on the ground.&lt;/li&gt;
&lt;li&gt;I could have used direct velocity manipulation or AddForce, depending on the specific requirements of my game and the level of control I needed over the physics simulation. while more complex scenarios may benefit from the physics engine's capabilities provided by AddForce, direct velocity manipulation is usually more suitable for simple 2D games with straightforward character movement.&lt;/li&gt;
&lt;li&gt;I also added a collision check to ensure the character jumps again only if it’s back to the Ground.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This is the "jump" action callback method
if(_isGrounded)
{
   _rigidbody.AddForce(new Vector2(0f, _jumpForce), ForceMode2D.Impulse);
   _isGrounded = false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;_isGrounded is set to true when the character collides with the Ground.&lt;/p&gt;
&lt;h3&gt;
  
  
  Fire
&lt;/h3&gt;

&lt;p&gt;As I mentioned in the beginning, the action type "Button" only has the "Performed" phase. So, to simulate the Started and Canceled callbacks, inside the OnFire() method, I set a condition to change on each "Performed" phase (once on pressing the key, and once on releasing it).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  void OnFire(InputAction.CallbackContext context)
  {
     if (context.performed)
     {
         // Button action Type only has Performed phase, once at pressing the key and once at release
         // so I flip the _isFiring value on each Performed callback to semutale Started and Canceled callbacks
         _isFiring = !_isFiring;
      }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fire() method instantiates the bullet prefabs at the spawn location and towards the parameter's direction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void Fire()
{
    // Determine the rotation based on the orientation of the fire point
    Quaternion bulletRotation = (_firePoint.localScale.x &amp;gt; 0) ? _firePoint.rotation : Quaternion.Euler(0f, 180f, 0f);

    // Instantiate(whatToSpawn, whereToSpawn, adjustedRotation)
    Instantiate(_bulletPrefab, _firePoint.position, bulletRotation);

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

&lt;/div&gt;



&lt;p&gt;Then, I set a timer reset to ensure there is a fixed time gap between each bullet spawn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    void FixedUpdate()
    {
        // Check if enough time has passed to fire another bullet
        _fireTimer += Time.fixedDeltaTime;

        if (_fireTimer &amp;gt;= _fireRate)
        {
            if(_isFiring)
                Fire();

            _fireTimer = 0f; // Reset the timer
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Unity's new Input System is a powerful feature that any 2D or 3D game can benefit from. I hope this blog gave you some inspiration. You might need to do your own little research to find the right workflow for your project. Thanks for reading! Feel free to leave me comments and questions.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The functionality of my Player GameObject in my first 3D game</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Tue, 28 Nov 2023 04:13:41 +0000</pubDate>
      <link>https://dev.to/atenajoon/the-functionality-of-my-player-gameobject-in-my-first-3d-game-148a</link>
      <guid>https://dev.to/atenajoon/the-functionality-of-my-player-gameobject-in-my-first-3d-game-148a</guid>
      <description>&lt;ul&gt;
&lt;li&gt;in the script, I used &lt;a href="https://docs.unity3d.com/ScriptReference/Input.html"&gt;Input.GetAxisRaw()&lt;/a&gt; method to make the Player object move around and jump.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I used Rigidbody's AddForce() method to make the object jump. Set the ForceMode to "Impulse" to make the jump instant with a small amount of force, and the Unity Built-in &lt;a href="https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html"&gt;OnCollisionEnter(Collision collision)&lt;/a&gt; method to make sure the player object jumps only when it's back on the ground, and I used a condition to specify the very first touch of the player to the ground is the one I'm looking for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// condition to check if the player has touched the ground
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.GetContact(0).normal == Vector3.up)
        {
            isGrounded = true;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; GetContact(0).normal&lt;br&gt;
"Contact" is when 2 collider objects hit each other. And if they have weird shapes and touch each other from a few spots, then we need to specify which contact we need to work on. That's why we are passing 0 to the GetContact() function to specify that we need the first contact. "normal" means we are getting the normal direction of the contact, which is basically the direction that the object we are hitting is facing. (if we hit the ground, then the ground is facing up, so the normal direction is up).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To make the game object faces the direction of its movement, we set the &lt;a href="https://docs.unity3d.com/ScriptReference/Transform-forward.html"&gt;transform.forward&lt;/a&gt; to rigidbody.velocity's x and z. (set the y to 0 to prevent the object facing up or down).&lt;/li&gt;
&lt;li&gt;When the Player object hits the enemies we want to restart the current scene and start over (Game Over). To do so, we use &lt;a href="https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html"&gt;SceneManager&lt;/a&gt;.LoadScene() method, which accepts the index of the scene we want it to load. However, since we don't know which scene at the time the player is going to be on, we just pass the index of the Scene in the Unity  Build Settings that "GetActiveScene().buildIndex" returns to us.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// restarts the current scene
    public void GameOver()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To help the Enemy and Bonfire objects know if the Player object overlaps them, I just added a tag to the Player object. Read about them &lt;a href="https://dev.to/atenajoon/crafting-my-first-3d-game-in-unity-4521-temp-slug-26410?preview=f6084ccc7b60ad9a8ce22e3e3b9974f309840d242920c3023f7ece4948abba1fee43f48228005be5325f0a7e0fec50b4d84301176d8f95621b0cdcb2"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The character is immune and blinks for 1.5 seconds after hitting a bonfire. I made a script named "BlinkGameObject" and added it to the HealthText and the Player objects. In the Bonfire.sc I check the conditions and call the Start and Stop the blink from the new script.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To Hide and Show the Player object (Blink) once it touches the Bonfires I just set its local scale to 0 and brought it to 1 for 1.5 seconds via the InvokeRepeating() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void CallStartBlinkGameObject()
    {
        InvokeRepeating("StartBlinkGameObject", 0f , blinkRepeatTime);   
    }

    void StartBlinkGameObject()
    {
        if(targetBlinkObject.transform.localScale == new Vector3(1, 1, 1)) {
            targetBlinkObject.transform.localScale = new Vector3(0, 0, 0);
        } else targetBlinkObject.transform.localScale = new Vector3(1, 1, 1);
    }
    public void StopBlinkGameObject()
    {
        CancelInvoke();
        targetBlinkObject.transform.localScale = new Vector3(1, 1, 1);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Crafting My First 3D Game in Unity</title>
      <dc:creator>Atena Razm</dc:creator>
      <pubDate>Tue, 28 Nov 2023 04:07:54 +0000</pubDate>
      <link>https://dev.to/atenajoon/crafting-my-first-3d-game-in-unity-2o96</link>
      <guid>https://dev.to/atenajoon/crafting-my-first-3d-game-in-unity-2o96</guid>
      <description>&lt;h2&gt;
  
  
  How did it all start?
&lt;/h2&gt;

&lt;p&gt;Well, the very first steps of making this game were based on the "Create Your First 3D Game in Unity" tutorial from Zenva Academy, with the intention of learning how to do it!&lt;br&gt;
After a few months, I added the new stuff I'd learned here and there from other websites to it. A few weeks ago, one of my developer friends played it on my computer and encouraged me to polish it and deploy it. He believed that I should publish a blog about it and get feedback on it from the people out there to make better games in the future. Here you go, help me improve my next game with your feedback!&lt;br&gt;
It's noteworthy that the whole purpose of making this game was to learn and use the essential and basic Unity assets, namespaces, classes, and build-in methods, not using fancy outstanding purchasable assets or expressing a wonderful game story. So, let's keep it simple, and I will define different projects for those matters soon!!&lt;br&gt;
Wanna give it a try? Play it &lt;a href="https://atenajoon.itch.io/levelup"&gt;here&lt;/a&gt;!&lt;/p&gt;
&lt;h2&gt;
  
  
  The Game Story and Rules
&lt;/h2&gt;

&lt;p&gt;In all of the levels, the player character needs to collect 20 coins and reach the end flag within 60 seconds to pass to the next level. By passing a level, the dangerous items and obstacles get increased. Some enemies eliminate the character, and others just reduce the health by 1.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Up &amp;amp; down arrow keys: moving the object forward &amp;amp; backward (Z-axis)&lt;/li&gt;
&lt;li&gt;Right &amp;amp; left arrow keys: moving right &amp;amp; left (X-axis)&lt;/li&gt;
&lt;li&gt;Space key: jumping up (Y-axis)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Elements and GameObjects I Created
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Scenes
&lt;/h3&gt;

&lt;p&gt;I created two level scenes, one starting menu scene, and one win menu scene. Loading different scenes is done by &lt;a href="https://docs.unity3d.com/ScriptReference/SceneManagement.Scene.html"&gt;SceneManager&lt;/a&gt;.LoadScene() method, which accepts the index of the scene we want it to load. Check out the SceneManager papular methods &lt;a href="https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Player Object
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The Player character is made with the assets that came with the tutorial.&lt;/li&gt;
&lt;li&gt;I made an empty object named Player, added the "advancedCharacter" Model to it as a child, and set the proper Scale (11, 11, 11) and position (0, 0, 0) for the best practice.&lt;/li&gt;
&lt;li&gt;To make the player object respect gravity and stay on the ground, I added a &lt;a href="https://docs.unity3d.com/ScriptReference/Collider.html"&gt;Collider&lt;/a&gt; component.&lt;/li&gt;
&lt;li&gt;To prevent the Player object from falling to the ground due to gravity I set the &lt;em&gt;Rigidbody Component &amp;gt;&amp;gt; Constraints tab &amp;gt;&amp;gt; "Freez Rotation" option &amp;gt;&amp;gt; check the X, Y, and Z checkboxes.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;a href="https://docs.unity3d.com/ScriptReference/Rigidbody.html"&gt;RigidBody&lt;/a&gt; component applies physics to a GameObject, and Constraints checkboxes can freeze a certain axis, either the position or the rotation axis, so that the physics won't be applied to it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create the "PlayerController" C# script, and attach it to the Player object to control its logic and functionality.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The functionality of the Player object is explained here in my &lt;a href="https://dev.to/atenajoon/the-functionality-of-my-player-gameobject-in-my-first-3d-game-1fo7-temp-slug-2349821?preview=11153d953810299f97ce7dada7ea647d9a4ba3ba04033abc85999954a6eb82fcd6c337565869894deff998b3434a2245448c9f7040dceb3fe523222c"&gt;&lt;strong&gt;&lt;em&gt;The functionality of my Player GameObject in my first 3D&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; game blog.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The offset property is set to the Main Camera to make a bit of space between the camera and the player object.&lt;/li&gt;
&lt;li&gt;I used Unity InputSystem to set the game for Gamepad, following &lt;a href="https://www.youtube.com/watch?v=p-3S73MaDP8"&gt;this tutorial&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Main Camera
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To make the camera follow the player object, Make it a child of the Player gameObject, and in its script file, set the transform.position equal to &lt;em&gt;target.position&lt;/em&gt;. (in Unity Inspector, assign the target property to the Player object)&lt;br&gt;
Offset property is set to the camera to make a bit of space between the camera and the player object.&lt;/p&gt;
&lt;h2&gt;
  
  
  Dangerous Terrian and Lava Hills
&lt;/h2&gt;

&lt;p&gt;This prefab is not part of the tutorial, I made it by myself. I got the lava material from the Unity Asset store &lt;a href="https://assetstore.unity.com/packages/2d/textures-materials/stylized-lava-materials-180943"&gt;here&lt;/a&gt;.&lt;br&gt;
Then I created a &lt;a href="https://docs.unity3d.com/Manual/terrain-UsingTerrains.html"&gt;Terrain&lt;/a&gt; GameObject, called LavaGround. In the Inspector, I picked the "Paint Terrian" brush (the second tab from the left side) and chose the "Paint Texture" option from the dropdown. Then clicked the "Edit Terrian Layers" and chose the Lava Asset I downloaded earlier into my materials folder. I also chose the "Rise or Lower Terrain" option to create the hills.&lt;br&gt;
Then I added the LavaGround script to it, and since the Terrain collider is different from the Enemy's box Collider, I used &lt;a href="https://docs.unity3d.com/ScriptReference/Collision-collider.html"&gt;Collision.collider&lt;/a&gt; method to check if the player touched the LavaGround. Read more about the Collision class &lt;a href="https://docs.unity3d.com/ScriptReference/Collision.html"&gt;here&lt;/a&gt;.&lt;br&gt;
I might make the lava ground move with an offset to make it more dynamic.&lt;/p&gt;
&lt;h2&gt;
  
  
  Leaderboard Canvas
&lt;/h2&gt;

&lt;p&gt;For the Leaderboard, I used &lt;a href="https://docs.unity3d.com/ScriptReference/Canvas.html"&gt;Canvas&lt;/a&gt;, which is a game object that contains all of the different UI elements that we wanna display on screen. It is supposed to show our collected Coins, Health, and Timer. Later on, I also added the Flashing "Game Over" message to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I might add a "Pause" and a "Speaker" button to it.&lt;/p&gt;

&lt;p&gt;The health text blinks on touching the bonfires. To do so, I kept switching its activation via &lt;em&gt;SetActive(true/false);&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If the player reaches the EndFlag before collecting all coins, the leftover coins' text flashes red for 1.5 seconds. For the red flash functionality, I made another exact same text object, placed it on top of the first one, and switched its scale between 0 and 1 on the desired moment via &lt;em&gt;GameObject.transform.localScale = new Vector3(0,0,0);&lt;/em&gt; &lt;a href="https://discussions.unity.com/t/how-can-i-hide-a-gameobject-without-active-false/64919"&gt;ref&lt;/a&gt;&lt;br&gt;
To add the color background, I had to add an "image" component to it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Timer
&lt;/h2&gt;

&lt;p&gt;TextMeshProUGUI is used to display the timer's characters. I followed &lt;a href="https://www.youtube.com/watch?v=27uKJvOpdYw"&gt;this tutorial&lt;/a&gt; to make the timer, and then I deactivated the minutes and separator components since the game round was just 60 seconds. I also added the "GameOver" object to the Timer script code and made it flash for 5 seconds along with 00 seconds, and then called the Start-Menu scene afterward. The Player object moveSpeed is set to 0 at GameOver time. To reach the Player's moveSpeed property from another component, I used: &lt;em&gt;&lt;a href="https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html"&gt;GameObject.FindWithTag&lt;/a&gt;("Player").GetComponent();&lt;/em&gt;&lt;br&gt;
The timer is set to 00:00 by default, but it's being reset at the Start() method. In the Update() method we keep reducing deltaTime from the timer as long as the timer is more than 0, and we call Flash() once it reaches 0. I used &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=net-5.0#Starting"&gt;String.Format&lt;/a&gt;() in UpdateTimerDisplay().&lt;/p&gt;
&lt;h2&gt;
  
  
  Enemies
&lt;/h2&gt;

&lt;p&gt;Enemy is a simple red cube from the same tutorial that moves back and forth between two positions with the specified speed. This game object passes through any other objects in the game but the Player. Touching the Enemy kills the Player and restarts the same scene.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the Update method of the Enemy script, we used the &lt;a href="https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html"&gt;MoveTowards()&lt;/a&gt; method to make the enemy object move between two positions.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Update is called once per frame
    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);

        if(transform.position == targetPos)
        {
            if(targetPos == startPos)
            {
                targetPos = startPos + moveOffset;
            }
            else
            {
                targetPos = startPos;
            }
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Speed is gonna move us at the rate of "speed" per frame. What I needed was moving 1 unit per second, not per frame. That's why I am multiplying speed by &lt;a href="https://docs.unity3d.com/ScriptReference/Time-deltaTime.html"&gt;Time.deltaTime&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the Player object overlaps an Enemy object, the &lt;em&gt;OnTriggerEnter()&lt;/em&gt; method is called.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Player"))
        {
            other.GetComponent&amp;lt;PlayerController&amp;gt;().GameOver();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; "other" is the object the Enemy hit, which is the Player object here. First I checked the object's tag to ensure it's the Player. Then from that object, I called the GameOver() method from the PlayerController component using &lt;a href="https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html"&gt;GetComponent&lt;/a&gt;.&lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; The &lt;a href="https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html"&gt;&lt;strong&gt;OnTriggerEnter(Collider)&lt;/strong&gt;&lt;/a&gt; method is very similar to the &lt;a href="https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html"&gt;&lt;strong&gt;OnCollisionEnter(Collision)&lt;/strong&gt;&lt;/a&gt;, but instead of detecting if two objects hit each other, it detects if they &lt;strong&gt;overlap&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bonfires and Smoke Particles
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Touching the bonfire objects reduces the "Health" by 1. This prefab was not part of the Zenva tutorial, I added it by myself.&lt;/li&gt;
&lt;li&gt;This object is created with 3 different objects:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tim&lt;/p&gt;

&lt;p&gt;FireParticles&lt;/p&gt;

&lt;p&gt;BurnEffect&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I have used Unity ParticleSystem to make the fire and the smoke. &lt;a href="https://www.youtube.com/watch?v=TEZm8cfJnDg"&gt;ref-1&lt;/a&gt;, &lt;a href="https://www.youtube.com/watch?v=FEA1wTMJAR0"&gt;ref-2&lt;/a&gt;, &lt;a href="https://www.youtube.com/watch?v=m0fjrQkaES4"&gt;ref-3&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The smoke &lt;a href="https://docs.unity3d.com/Manual/PartSysTriggersModule.html"&gt;Triggers&lt;/a&gt; in case of Player colliding. Learn more about the ParticleSystem &lt;a href="https://learn.unity.com/tutorial/introduction-to-particle-systems"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Coins
&lt;/h2&gt;

&lt;p&gt;The coin object was created the same way as the Enemy object and got a script assigned to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void Update()
    {
        transform.Rotate(Vector3.up, rotateSpeed * Time.deltaTime);

    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Player"))
        {
            other.GetComponent&amp;lt;PlayerController&amp;gt;().TotalCoins(1);
            Destroy(gameObject);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Since I wanted the coin to rotate around the Y axis, I passed  "Vector3.up" as the first argument and the desired rotation speed as the second argument to the &lt;strong&gt;transform.Rotate()&lt;/strong&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Level Creation
&lt;/h2&gt;

&lt;p&gt;Before making the levels, I made all of the mutually used game objects like the Player, Enemy, Count, and MainCamera prefabs, so that I can use them all in all of the levels without having to remake them over and over.&lt;/p&gt;

&lt;p&gt;Then I made the Platforms prefabs and added them to the scene. Then I set the lighting:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Click the Auto Generate Lighting to open the lighting window&lt;/li&gt;
&lt;li&gt;Make a New lighting&lt;/li&gt;
&lt;li&gt;Click the Auto Generate checkbox&lt;/li&gt;
&lt;li&gt;You can also play around with the Directional Light&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79gg98c9j3tomba61z0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F79gg98c9j3tomba61z0l.png" alt="Image description" width="800" height="423"&gt;&lt;/a&gt;&lt;br&gt;
Then I made the EndFlag prefab.&lt;/p&gt;

&lt;h2&gt;
  
  
  EndFlag
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Made the EndFlag prefab&lt;/li&gt;
&lt;li&gt;Created the Level-2 scene&lt;/li&gt;
&lt;li&gt;Added the second scene to the scenes list on:
File &amp;gt;&amp;gt; Build Settings…
Drag and drop the scenes into the "Scenes in Build" list&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The scenes are being called based on their index number in the list.&lt;/p&gt;

&lt;p&gt;Then in the EndFlag script, I set conditions to change the level in case the Player object collides with the flag, and all of the coins are collected. &lt;a href="https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html"&gt;SceneManager&lt;/a&gt;.LoadScene() method can accept the scene index number or its name as an argument. Read more &lt;a href="https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start Menu
&lt;/h2&gt;

&lt;p&gt;For each menu scene, I created a separate canvas to display. In each menu scene's Main camera inspector, I changed the skybox option to a solid color. Then I added 1 TextMashPro and 2 Button-TextMashPro objects from the UI tab, named "Play", and "Quit!".&lt;br&gt;
Each of them is calling a method on the Menu script, which is attached to our canvas. To do so, I dragged and dropped the Canvas object to the onClick event listener in the inspector, and picked the relevant method for each button.&lt;/p&gt;

&lt;h2&gt;
  
  
  Audio &amp;amp; Sound Management
&lt;/h2&gt;

&lt;p&gt;Following &lt;a href="https://www.youtube.com/watch?v=eNi67i5my84"&gt;this tutorial&lt;/a&gt;, we need 3 things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating an empty object called AudioManager&lt;/li&gt;
&lt;li&gt;Sound Source Component (in the AudioManager EmptyObject)&lt;/li&gt;
&lt;li&gt;Audio Listener Component (in the Main Camera Object)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I recorded my own voice with Mac's VoiceMemo application and converted the m4a file to mp3 on &lt;a href="https://cloudconvert.com/m4a-to-mp3"&gt;CloudConvert&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build and Deployment
&lt;/h2&gt;

&lt;p&gt;I built the app locally and published it on &lt;a href="https://itch.io/"&gt;itch.io&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Improvement Ideas &amp;amp; Optimization
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Adding some movement and animation to the dangerous ground and the Player game object&lt;/li&gt;
&lt;li&gt;Adding more sound effects to the Player's movements and background music&lt;/li&gt;
&lt;li&gt;using more decent audio and material assets from the Unity Asset Store to give it a more professional look&lt;/li&gt;
&lt;li&gt;Adding more enemies for the next levels and increasing the type of challenges&lt;/li&gt;
&lt;li&gt;Adding the "Pause" and "Mute" buttons to the Leaderboard.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Making my first 3D game in Unity was not just about pixels and code; it was about getting real and trusting the little voice in my head saying: "Just do it, it'll get easier next time!". While the initial intention of crafting this little game was to showcase what I knew about Unity and game development, it actually opened my eyes to the world of my unknowns. As a result, now I have a longer To_Learn list!&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>programming</category>
      <category>beginners</category>
      <category>programmers</category>
    </item>
  </channel>
</rss>
