{"id":31953,"date":"2024-11-01T09:04:28","date_gmt":"2024-11-01T09:04:28","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31953"},"modified":"2024-11-01T11:33:59","modified_gmt":"2024-11-01T11:33:59","slug":"unity-beginner-course-state-transition-based-on-hit","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31953\/","title":{"rendered":"Unity Beginner Course: State Transition Based on Hit"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this tutorial, we will take a detailed look at how to switch a character&#8217;s state when hit using Unity. Hit processing is a very important element in game development, as it enhances the fun and immersion of the game. This tutorial will start from the basic concepts of implementing a hit system and guide you step by step on how to implement hit states in Unity.<\/p>\n<h2>1. Overview of Hit System<\/h2>\n<p>The hit system determines what state a character will transition to when attacked. Effective hit processing significantly affects the user experience of the game, so it is important to design and implement it correctly. Generally, the hit system considers the following elements:<\/p>\n<ul>\n<li><strong>Hit Detection:<\/strong> Criteria for determining if a character can be hit.<\/li>\n<li><strong>State Transition:<\/strong> Changes in the character&#8217;s state after being hit (e.g., idle state, staggered state).<\/li>\n<li><strong>Hit Effects:<\/strong> Visual\/auditory effects that occur when hit.<\/li>\n<li><strong>Health System:<\/strong> Functionality to manage the character&#8217;s health.<\/li>\n<\/ul>\n<h2>2. Implementing Hit Detection System<\/h2>\n<p>The hit detection system serves to detect when a character has been attacked. In Unity, you can implement hit detection through a collision system. Here are the basic settings for hit detection:<\/p>\n<h3>2.1 Collision Setup<\/h3>\n<p>To detect hits using Unity&#8217;s physics system, you need to use Collider and Rigidbody components to detect collisions between characters and enemies. The collider is a shape used to detect collisions, which can be added to the character and enemy objects in Unity&#8217;s Inspector window.<\/p>\n<pre><code>using UnityEngine;\n\npublic class Player : MonoBehaviour\n{\n    private void OnTriggerEnter(Collider other)\n    {\n        if (other.CompareTag(\"Enemy\"))\n        {\n            \/\/ Handle hit\n            TakeDamage();\n        }\n    }\n\n    private void TakeDamage()\n    {\n        \/\/ Process health decrease and state transition\n        Debug.Log(\"Hit!\");\n    }\n}\n<\/code><\/pre>\n<h3>2.2 Tag Setup<\/h3>\n<p>The enemy object should be tagged as &#8220;Enemy&#8221; to ensure it collides with the correct object. Tags can be easily set in Unity&#8217;s Inspector.<\/p>\n<h2>3. Implementing State Transition Logic<\/h2>\n<p>You need to decide how to transition the character&#8217;s state when hit. Common states include:<\/p>\n<ul>\n<li><strong>Normal State:<\/strong> The character is in an unharmed state.<\/li>\n<li><strong>Hit State:<\/strong> The character&#8217;s reaction state after being hit.<\/li>\n<li><strong>Dead State:<\/strong> The state after losing all health.<\/li>\n<\/ul>\n<h3>3.1 Defining State Enum<\/h3>\n<p>First, define an Enum to manage the character&#8217;s states. This will make it easier to manage state transitions.<\/p>\n<pre><code>public enum PlayerState\n{\n    Normal,\n    Hit,\n    Dead\n}\n<\/code><\/pre>\n<h3>3.2 Adding State Variable<\/h3>\n<p>Now, add a state variable to the character class. Implement different behaviors based on the state.<\/p>\n<pre><code>private PlayerState currentState = PlayerState.Normal;\n\nprivate void Update()\n{\n    switch (currentState)\n    {\n        case PlayerState.Normal:\n            \/\/ Behavior in normal state\n            break;\n        case PlayerState.Hit:\n            \/\/ Behavior in hit state\n            break;\n        case PlayerState.Dead:\n            \/\/ Behavior in dead state\n            break;\n    }\n}\n<\/code><\/pre>\n<h2>4. Hit Effects and Animations<\/h2>\n<p>Adding visual effects when hit can enhance the player&#8217;s immersion. Let&#8217;s explore how to add animations and effects when hit.<\/p>\n<h3>4.1 Hit Animation<\/h3>\n<p>Set up hit animations using Animator. Configure it to play the animation when entering the hit state.<\/p>\n<pre><code>private Animator animator;\n\nprivate void Start()\n{\n    animator = GetComponent<Animator>();\n}\n\nprivate void TakeDamage()\n{\n    \/\/ Transition animation\n    animator.SetTrigger(\"Hit\");\n    currentState = PlayerState.Hit;\n}\n<\/code><\/pre>\n<h3>4.2 Hit Effect<\/h3>\n<p>A Visual Effect Asset is needed. Set the necessary Asset as a Prefab to play the effect.<\/p>\n<pre><code>public GameObject hitEffect;\n\nprivate void TakeDamage()\n{\n    \/\/ Transition animation\n    animator.SetTrigger(\"Hit\");\n\n    \/\/ Create effect\n    Instantiate(hitEffect, transform.position, Quaternion.identity);\n\n    currentState = PlayerState.Hit;\n}\n<\/code><\/pre>\n<h2>5. Implementing the Health System<\/h2>\n<p>The health system is a key element that determines the survival of the character. Let&#8217;s implement how to manage health and decrease it when hit.<\/p>\n<h3>5.1 Defining Health Variable<\/h3>\n<p>First, define a health variable, and ensure the character transitions to a dead state when the health reaches 0.<\/p>\n<pre><code>private int maxHealth = 100;\nprivate int currentHealth;\n\nprivate void Start()\n{\n    currentHealth = maxHealth;\n}\n\nprivate void TakeDamage(int damage)\n{\n    currentHealth -= damage;\n\n    if (currentHealth <= 0)\n    {\n        currentState = PlayerState.Dead;\n        \/\/ Call death handling\n        Die();\n    }\n}\n<\/code><\/pre>\n<h3>5.2 Health Recovery System<\/h3>\n<p>Adding a functionality to recover health can add depth to the game. Implement health recovery items to make use of this feature.<\/p>\n<h2>6. Time Control in Hit State<\/h2>\n<p>Controlling actions while in a hit state is very important. After a certain amount of time in the hit state, the character should return to the normal state.<\/p>\n<h3>6.1 Using Coroutines<\/h3>\n<p>To maintain the hit state for a certain duration, use coroutines. Return to the normal state after a specified time.<\/p>\n<pre><code>private IEnumerator HitCoroutine()\n{\n    yield return new WaitForSeconds(1f); \/\/ Maintain hit state for 1 second\n    currentState = PlayerState.Normal;\n}\n<\/code><\/pre>\n<h2>7. Final Summary and Additional Considerations<\/h2>\n<p>Now the basic implementation of the hit system is complete. You can enhance the game's fun by adding various elements. For example:<\/p>\n<ul>\n<li>Add hit sound effects<\/li>\n<li>Diversity of hit animations<\/li>\n<li>Implement special effects for different states<\/li>\n<\/ul>\n<p>You can also implement various ideas based on this foundation. This will help you develop a more complete game.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we learned how to implement a state transition system based on hits using Unity. I hope this helped you understand the structure of a basic hit system through health management, animations, effects, and state transitions. Now, go ahead and implement a great hit system in your game!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this tutorial, we will take a detailed look at how to switch a character&#8217;s state when hit using Unity. Hit processing is a very important element in game development, as it enhances the fun and immersion of the game. This tutorial will start from the basic concepts of implementing a hit system and &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31953\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Unity Beginner Course: State Transition Based on Hit&#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-31953","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 Beginner Course: State Transition Based on Hit - \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\/31953\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unity Beginner Course: State Transition Based on Hit - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this tutorial, we will take a detailed look at how to switch a character&#8217;s state when hit using Unity. Hit processing is a very important element in game development, as it enhances the fun and immersion of the game. This tutorial will start from the basic concepts of implementing a hit system and &hellip; \ub354 \ubcf4\uae30 &quot;Unity Beginner Course: State Transition Based on Hit&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31953\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:04:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:33:59+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\/31953\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31953\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Unity Beginner Course: State Transition Based on Hit\",\"datePublished\":\"2024-11-01T09:04:28+00:00\",\"dateModified\":\"2024-11-01T11:33:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31953\/\"},\"wordCount\":676,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31953\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31953\/\",\"name\":\"Unity Beginner Course: State Transition Based on Hit - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:04:28+00:00\",\"dateModified\":\"2024-11-01T11:33:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31953\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31953\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31953\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unity Beginner Course: State Transition Based on Hit\"}]},{\"@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 Beginner Course: State Transition Based on Hit - \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\/31953\/","og_locale":"ko_KR","og_type":"article","og_title":"Unity Beginner Course: State Transition Based on Hit - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this tutorial, we will take a detailed look at how to switch a character&#8217;s state when hit using Unity. Hit processing is a very important element in game development, as it enhances the fun and immersion of the game. This tutorial will start from the basic concepts of implementing a hit system and &hellip; \ub354 \ubcf4\uae30 \"Unity Beginner Course: State Transition Based on Hit\"","og_url":"https:\/\/atmokpo.com\/w\/31953\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:04:28+00:00","article_modified_time":"2024-11-01T11:33:59+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\/31953\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31953\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Unity Beginner Course: State Transition Based on Hit","datePublished":"2024-11-01T09:04:28+00:00","dateModified":"2024-11-01T11:33:59+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31953\/"},"wordCount":676,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31953\/","url":"https:\/\/atmokpo.com\/w\/31953\/","name":"Unity Beginner Course: State Transition Based on Hit - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:04:28+00:00","dateModified":"2024-11-01T11:33:59+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31953\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31953\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31953\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Unity Beginner Course: State Transition Based on Hit"}]},{"@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\/31953","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=31953"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31953\/revisions"}],"predecessor-version":[{"id":31954,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31953\/revisions\/31954"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}