{"id":31848,"date":"2024-11-01T09:03:27","date_gmt":"2024-11-01T09:03:27","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31848"},"modified":"2024-11-01T11:34:27","modified_gmt":"2024-11-01T11:34:27","slug":"titleunity-basics-course-creating-function-by-state","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31848\/","title":{"rendered":"title>Unity Basics Course: Creating Function by State<\/title"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this tutorial, we will learn how to create state-based functions in Unity. Unity is an excellent game engine, but its usage can be somewhat difficult for beginners. This tutorial will specifically explain the concepts of basic state management and the process of creating state-based functions.<\/p>\n<h2>1. What is a State?<\/h2>\n<p>A state represents the current situation of an object, determining how various elements of the game behave and interact. For example, there are various states such as the character sitting, walking, or jumping. States are an important concept in defining events and actions in your game.<\/p>\n<h3>1.1 Importance of States<\/h3>\n<p>Through state management, we can define various behaviors and actions within the game. Since the actions a character can perform change based on the state, it significantly impacts the flow of the game and the user experience.<\/p>\n<h2>2. Creating State-Based Functions<\/h2>\n<p>Now, let&#8217;s learn how to create state-based functions in Unity. We will create a simple character controller for this purpose.<\/p>\n<h3>2.1 Creating a New Script<\/h3>\n<p>Create a new C# script in the Unity editor. Let&#8217;s name it <code>CharacterController<\/code>. Add the following code to the script:<\/p>\n<pre><code>using UnityEngine;\n\npublic class CharacterController : MonoBehaviour\n{\n    private enum State { Idle, Walking, Jumping }\n    private State currentState = State.Idle;\n\n    private void Update()\n    {\n        switch (currentState)\n        {\n            case State.Idle:\n                HandleIdleState();\n                break;\n            case State.Walking:\n                HandleWalkingState();\n                break;\n            case State.Jumping:\n                HandleJumpingState();\n                break;\n        }\n    }\n\n    private void HandleIdleState()\n    {\n        \/\/ Idle state logic\n        if (Input.GetKeyDown(KeyCode.W))\n        {\n            currentState = State.Walking;\n        }\n        else if (Input.GetKeyDown(KeyCode.Space))\n        {\n            currentState = State.Jumping;\n        }\n    }\n\n    private void HandleWalkingState()\n    {\n        \/\/ Walking state logic\n        if (Input.GetKeyDown(KeyCode.S))\n        {\n            currentState = State.Idle;\n        }\n        else if (Input.GetKeyDown(KeyCode.Space))\n        {\n            currentState = State.Jumping;\n        }\n        \/\/ Implement walking movement\n    }\n\n    private void HandleJumpingState()\n    {\n        \/\/ Jumping state logic\n        \/\/ Set to return to idle state after jumping is done\n        currentState = State.Idle;\n    }\n}\n<\/code><\/pre>\n<h3>2.2 Explanation<\/h3>\n<p>In the above code, we define three states: Idle, Walking, and Jumping using an enum named <code>State<\/code>. The <code>currentState<\/code> variable manages the current state. The <code>Update<\/code> method calls appropriate functions (e.g., <code>HandleIdleState<\/code>, <code>HandleWalkingState<\/code>, <code>HandleJumpingState<\/code>) based on the current state.<\/p>\n<h3>2.3 State Transition<\/h3>\n<p>State transitions occur based on user input. In the idle state, you can press the W key to transition to the walking state, and the Space key to transition to the jumping state. In the walking state, you can press the S key to return to the idle state, and the Space key to transition to the jumping state. In the jumping state, it generally returns to the idle state after the jump is completed.<\/p>\n<h2>3. Advantages of State-Based Functions<\/h2>\n<p>There are several advantages to using state-based functions:<\/p>\n<ul>\n<li><strong>Improved Code Readability:<\/strong> By separating the code based on states, the logic for each state is clearly defined.<\/li>\n<li><strong>Ease of Maintenance:<\/strong> It becomes easier to add or modify functionality based on states.<\/li>\n<li><strong>Scalability:<\/strong> Adding new states becomes straightforward, making it advantageous when expanding the game&#8217;s features.<\/li>\n<\/ul>\n<h2>4. Practice: Adding State-Based Functions<\/h2>\n<p>Now, try adding your own character states. For example, add a new state called &#8220;Running&#8221; and implement transitioning to that state when the user presses the Shift key.<\/p>\n<pre><code>private enum State { Idle, Walking, Jumping, Running }\n\n\/\/ Add the following logic in the appropriate part of the code\nif (Input.GetKeyDown(KeyCode.LeftShift))\n{\n    currentState = State.Running;\n}\n\n\/\/ Running logic\nprivate void HandleRunningState()\n{\n    \/\/ Running logic\n   \n    if (Input.GetKeyDown(KeyCode.S))\n    {\n        currentState = State.Idle;\n    }\n    \/\/ ...\n}\n<\/code><\/pre>\n<h2>5. Conclusion<\/h2>\n<p>In this tutorial, we learned about creating state-based functions in Unity. State management is a crucial aspect of game development, and a well-designed state system increases code readability and ease of maintenance. Implement various states to enrich your own game!<\/p>\n<p>Thank you. See you in the next tutorial!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this tutorial, we will learn how to create state-based functions in Unity. Unity is an excellent game engine, but its usage can be somewhat difficult for beginners. This tutorial will specifically explain the concepts of basic state management and the process of creating state-based functions. 1. What is a State? A state represents &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31848\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;title>Unity Basics Course: Creating Function by State<\/title\"<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[135],"tags":[],"class_list":["post-31848","post","type-post","status-publish","format-standard","hentry","category-unity-basic"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>title&gt;Unity Basics Course: Creating Function by State<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/31848\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"title&gt;Unity Basics Course: Creating Function by State\" \/>\n<meta property=\"og:description\" content=\"Hello! In this tutorial, we will learn how to create state-based functions in Unity. Unity is an excellent game engine, but its usage can be somewhat difficult for beginners. This tutorial will specifically explain the concepts of basic state management and the process of creating state-based functions. 1. What is a State? A state represents &hellip; \ub354 \ubcf4\uae30 &quot;title&gt;Unity Basics Course: Creating Function by State\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31848\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:03:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:34:27+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31848\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31848\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"title>Unity Basics Course: Creating Function by State\",\"datePublished\":\"2024-11-01T09:03:27+00:00\",\"dateModified\":\"2024-11-01T11:34:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31848\/\"},\"wordCount\":8,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31848\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31848\/\",\"name\":\"title>Unity Basics Course: Creating Function by State\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:03:27+00:00\",\"dateModified\":\"2024-11-01T11:34:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31848\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31848\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31848\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"title>Unity Basics Course: Creating Function by State\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"title>Unity Basics Course: Creating Function by State","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/31848\/","og_locale":"ko_KR","og_type":"article","og_title":"title>Unity Basics Course: Creating Function by State","og_description":"Hello! In this tutorial, we will learn how to create state-based functions in Unity. Unity is an excellent game engine, but its usage can be somewhat difficult for beginners. This tutorial will specifically explain the concepts of basic state management and the process of creating state-based functions. 1. What is a State? A state represents &hellip; \ub354 \ubcf4\uae30 \"title>Unity Basics Course: Creating Function by State","og_url":"https:\/\/atmokpo.com\/w\/31848\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:03:27+00:00","article_modified_time":"2024-11-01T11:34:27+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31848\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31848\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"title>Unity Basics Course: Creating Function by State","datePublished":"2024-11-01T09:03:27+00:00","dateModified":"2024-11-01T11:34:27+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31848\/"},"wordCount":8,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31848\/","url":"https:\/\/atmokpo.com\/w\/31848\/","name":"title>Unity Basics Course: Creating Function by State","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:03:27+00:00","dateModified":"2024-11-01T11:34:27+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31848\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31848\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31848\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"title>Unity Basics Course: Creating Function by State"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=31848"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31848\/revisions"}],"predecessor-version":[{"id":31849,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31848\/revisions\/31849"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}