{"id":31854,"date":"2024-11-01T09:03:29","date_gmt":"2024-11-01T09:03:29","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31854"},"modified":"2024-11-01T11:34:25","modified_gmt":"2024-11-01T11:34:25","slug":"unity-basic-course-finite-state-machine","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31854\/","title":{"rendered":"Unity Basic Course: Finite State Machine"},"content":{"rendered":"<p>Unity is one of the most popular engines for game development, helping many developers easily implement complex game logic and behaviors. In this course, we will introduce the important concept of Finite State Machine (FSM) and take a closer look at how to implement it in Unity.<\/p>\n<h2>1. What is a Finite State Machine (FSM)?<\/h2>\n<p>A Finite State Machine (FSM) is a way of modeling a system that transitions from one state to another. In general, an FSM consists of the following elements:<\/p>\n<ul>\n<li><strong>States:<\/strong> Refers to each individual state that the system can be in.<\/li>\n<li><strong>Transitions:<\/strong> Conditions or events that occur when moving from one state to another.<\/li>\n<li><strong>Events:<\/strong> Triggers that cause a state change.<\/li>\n<\/ul>\n<p>FSMs are mainly utilized in various fields such as game AI, character animation, and user interfaces.<\/p>\n<h2>2. Basic Structure of a Finite State Machine<\/h2>\n<p>An FSM is composed of a set of states and state transitions. Essentially, an FSM starts from an initial state and can transition to various other states. Below is a simple diagram explaining the general structure of an FSM:<\/p>\n<pre>\n[State A] ----(Event 1)----&gt; [State B]\n             \\\n              \\----(Event 2)----&gt; [State C]\n<\/pre>\n<p>Here, &#8220;Event 1&#8221; or &#8220;Event 2&#8221; serves as the trigger for the state transitions. By defining the possible transitions from each state, you can control the behavior of the system.<\/p>\n<h2>3. Example of a Finite State Machine<\/h2>\n<p>A simple example in Unity could be the &#8220;player character&#8217;s state.&#8221; The player character can have the following states:<\/p>\n<ul>\n<li>Idle<\/li>\n<li>Walking<\/li>\n<li>Running<\/li>\n<li>Jumping<\/li>\n<\/ul>\n<p>Transitions between each state occur through events such as &#8220;switching to the walking state when the &#8216;W&#8217; key is pressed&#8221; or &#8220;switching to the jumping state when the jump button is pressed.&#8221;<\/p>\n<h2>4. Implementing FSM in Unity<\/h2>\n<p>To implement FSM in Unity, you can use C# scripts. Below is a basic script example for creating a finite state machine.<\/p>\n<pre>\nusing UnityEngine;\n\npublic enum PlayerState\n{\n    Idle,\n    Walking,\n    Running,\n    Jumping\n}\n\npublic class PlayerController : MonoBehaviour\n{\n    private PlayerState currentState;\n\n    private void Start()\n    {\n        currentState = PlayerState.Idle;\n    }\n\n    private void Update()\n    {\n        switch (currentState)\n        {\n            case PlayerState.Idle:\n                HandleIdle();\n                break;\n            case PlayerState.Walking:\n                HandleWalking();\n                break;\n            case PlayerState.Running:\n                HandleRunning();\n                break;\n            case PlayerState.Jumping:\n                HandleJumping();\n                break;\n        }\n    }\n\n    private void HandleIdle()\n    {\n        if (Input.GetKeyDown(KeyCode.W))\n        {\n            currentState = PlayerState.Walking;\n        }\n    }\n\n    private void HandleWalking()\n    {\n        if (Input.GetKeyDown(KeyCode.LeftShift))\n        {\n            currentState = PlayerState.Running;\n        }\n        else if (Input.GetKeyDown(KeyCode.Space))\n        {\n            currentState = PlayerState.Jumping;\n        }\n    }\n\n    private void HandleRunning()\n    {\n        if (Input.GetKeyUp(KeyCode.LeftShift))\n        {\n            currentState = PlayerState.Walking;\n        }\n        else if (Input.GetKeyDown(KeyCode.Space))\n        {\n            currentState = PlayerState.Jumping;\n        }\n    }\n\n    private void HandleJumping()\n    {\n        \/\/ Implement Jump logic here\n        currentState = PlayerState.Idle; \/\/ For example, logic to return to idle state after jumping\n    }\n}\n<\/pre>\n<h2>5. Managing Complexity of State Transitions<\/h2>\n<p>As state transitions become more complex, they can become difficult to manage. In such cases, several techniques can be employed to make the FSM more flexible:<\/p>\n<ul>\n<li><strong>State Manager:<\/strong> Separate the state transition logic into a separate class or manager to enhance maintainability.<\/li>\n<li><strong>Event System:<\/strong> Trigger state transitions through events to reduce code coupling and increase flexibility.<\/li>\n<li><strong>State Pattern:<\/strong> Classify each state to implement the logic of each state independently.<\/li>\n<\/ul>\n<h2>6. Implementing State Pattern<\/h2>\n<p>By using the state pattern, you can implement each state as a class, allowing for simple changes in logic depending on the state. Below is an example of implementation using the state pattern:<\/p>\n<pre>\npublic interface IPlayerState\n{\n    void Enter();\n    void Update();\n    void Exit();\n}\n\npublic class IdleState : IPlayerState\n{\n    public void Enter() { Debug.Log(\"Enter Idle State\"); }\n    public void Update() { \/* Idle Logic *\/ }\n    public void Exit() { Debug.Log(\"Exit Idle State\"); }\n}\n\n\/\/ Add WalkingState, RunningState, JumpingState classes here.\n<\/pre>\n<p>In this way, you can create each state implementation and write code to transition to the appropriate state in a superclass for player state management.<\/p>\n<h2>7. Utilizing Finite State Machines<\/h2>\n<p>Through FSMs, you can neatly structure and manage complex game logic. Additionally, they can be used in various areas such as AI behaviors, NPC dialogue systems, and state management for mini-games. Finite State Machines are a powerful and useful tool in game development.<\/p>\n<h2>8. Conclusion<\/h2>\n<p>In this course, we explored the concept of finite state machines and how to implement them within Unity. FSMs are essential elements in game development, enabling a better understanding and implementation of more complex and rich game mechanics. In the next course, we will expand on FSMs to learn about various game design patterns and their application in Unity.<\/p>\n<p>As we conclude the Unity basics course, I hope this lesson has been helpful in your game development journey. If you have any questions or feedback, please feel free to leave a comment!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unity is one of the most popular engines for game development, helping many developers easily implement complex game logic and behaviors. In this course, we will introduce the important concept of Finite State Machine (FSM) and take a closer look at how to implement it in Unity. 1. What is a Finite State Machine (FSM)? &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31854\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Unity Basic Course: Finite State Machine&#8221;<\/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-31854","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>Unity Basic Course: Finite State Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/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\/31854\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unity Basic Course: Finite State Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Unity is one of the most popular engines for game development, helping many developers easily implement complex game logic and behaviors. In this course, we will introduce the important concept of Finite State Machine (FSM) and take a closer look at how to implement it in Unity. 1. What is a Finite State Machine (FSM)? &hellip; \ub354 \ubcf4\uae30 &quot;Unity Basic Course: Finite State Machine&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31854\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:03:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:34:25+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31854\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31854\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Unity Basic Course: Finite State Machine\",\"datePublished\":\"2024-11-01T09:03:29+00:00\",\"dateModified\":\"2024-11-01T11:34:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31854\/\"},\"wordCount\":573,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31854\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31854\/\",\"name\":\"Unity Basic Course: Finite State Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:03:29+00:00\",\"dateModified\":\"2024-11-01T11:34:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31854\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31854\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31854\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unity Basic Course: Finite State Machine\"}]},{\"@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":"Unity Basic Course: Finite State Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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\/31854\/","og_locale":"ko_KR","og_type":"article","og_title":"Unity Basic Course: Finite State Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Unity is one of the most popular engines for game development, helping many developers easily implement complex game logic and behaviors. In this course, we will introduce the important concept of Finite State Machine (FSM) and take a closer look at how to implement it in Unity. 1. What is a Finite State Machine (FSM)? &hellip; \ub354 \ubcf4\uae30 \"Unity Basic Course: Finite State Machine\"","og_url":"https:\/\/atmokpo.com\/w\/31854\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:03:29+00:00","article_modified_time":"2024-11-01T11:34:25+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31854\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31854\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Unity Basic Course: Finite State Machine","datePublished":"2024-11-01T09:03:29+00:00","dateModified":"2024-11-01T11:34:25+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31854\/"},"wordCount":573,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31854\/","url":"https:\/\/atmokpo.com\/w\/31854\/","name":"Unity Basic Course: Finite State Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:03:29+00:00","dateModified":"2024-11-01T11:34:25+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31854\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31854\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31854\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Unity Basic Course: Finite State Machine"}]},{"@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\/31854","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=31854"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31854\/revisions"}],"predecessor-version":[{"id":31855,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31854\/revisions\/31855"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}