{"id":845,"date":"2022-11-18T16:10:10","date_gmt":"2022-11-18T16:10:10","guid":{"rendered":"https:\/\/globalgoodplay.com\/?p=845"},"modified":"2023-04-13T08:47:38","modified_gmt":"2023-04-13T08:47:38","slug":"c-interfaces-in-unity-create-games-the-easy-way","status":"publish","type":"post","link":"https:\/\/globalgoodplay.com\/?p=845","title":{"rendered":"C# Interfaces In Unity \u2013 Create Games The Easy Way"},"content":{"rendered":"<div class=\"elementor elementor-32098\" data-elementor-type=\"wp-post\" data-elementor-id=\"32098\">\n<section class=\"elementor-section elementor-top-section elementor-element elementor-element-9f75023 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"9f75023\" data-element_type=\"section\" data-settings=\"{\">\n<div class=\"elementor-container elementor-column-gap-default\">\n<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-5edd7b5\" data-id=\"5edd7b5\" data-element_type=\"column\">\n<div class=\"elementor-widget-wrap elementor-element-populated\">\n<div class=\"elementor-element elementor-element-38b5bb0 elementor-widget elementor-widget-text-editor\" data-id=\"38b5bb0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">When creating a game in Unity the most important thing is to structure your project in a way that you can expand it later by adding new updates to the game.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This is done from the beginning when you start creating parts of your game, not like what you see on all YouTube tutorials and low-quality courses where they just open a project and start creating things at random.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If you don\u2019t have a proper structure and you decide after 6 months to add a new feature in your game, it can break other features, and then you\u2019ll have to deal with bugs that are hard to solve.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">And what\u2019s worse, if you use a project with a bad structure in your portfolio and the game studio you applied for sees that, it\u2019s an immediate rejection.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To solve this problem we use Object Oriented programming and design patterns principles. And one concept that helps us implement that is called Interface.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Before we start, this tutorial is not for complete beginners. If you don\u2019t know how to create basic games with Unity and you\u2019re not familiar what are classes, variables, and functions, you should first go through our C# beginner series and then come back to this lecture.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can check out our C# beginner series by clicking here.<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-b6e992d elementor-widget elementor-widget-template\" data-id=\"b6e992d\" data-element_type=\"widget\" data-widget_type=\"template.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"elementor-template\">\n<div class=\"elementor elementor-31169\" data-elementor-type=\"section\" data-elementor-id=\"31169\">\n<section class=\"elementor-section elementor-top-section elementor-element elementor-element-7eaceb00 elementor-section-full_width elementor-section-height-default elementor-section-height-default\" data-id=\"7eaceb00\" data-element_type=\"section\" data-settings=\"{\">\n<div class=\"elementor-container elementor-column-gap-default\">\n<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-7542c05a\" data-id=\"7542c05a\" data-element_type=\"column\">\n<div class=\"elementor-widget-wrap elementor-element-populated\">\n<div class=\"elementor-element elementor-element-7a156fc9 elementor-widget elementor-widget-ucaddon_ue_random_image\" data-id=\"7a156fc9\" data-element_type=\"widget\" data-widget_type=\"ucaddon_ue_random_image.default\">\n<div class=\"elementor-widget-container\"><!-- start Random Image - Middle --><\/p>\n<style type=\"text\/css\">\/* widget: Random Image - Middle *\/<\/p>\n<p>#uc_ue_random_image_elementor476765<br \/>\n{<br \/>\n  display:flex;<br \/>\n}<\/p>\n<p>#uc_ue_random_image_elementor476765 .ue-image-item<br \/>\n{<br \/>\n  display:block;<br \/>\n  transition:0.3s;<br \/>\n  width:100%;<br \/>\n}<\/p>\n<p>#uc_ue_random_image_elementor476765 .ue-image-link<br \/>\n{<br \/>\n  display:block;<br \/>\n  color: unset;<br \/>\n}<\/p>\n<\/style>\n<div id=\"uc_ue_random_image_elementor476765\" class=\"ue-random-image\" data-debug=\"false\" data-editor=\"no\">\n<div class=\"ue-image\">\n<div class=\"ue-image-box\">\n<p><img class=\"ue-image-item\" \/><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!-- end Random Image - Middle --><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/section>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-65096be elementor-widget elementor-widget-heading\" data-id=\"65096be\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">What Is A C# Interface And How Do We Create One?<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-ccd9487 elementor-widget elementor-widget-text-editor\" data-id=\"ccd9487\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">An Interface is a container that holds functions that can have parameters but are empty e.g. not implemented inside the Interface itself.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The functions are implemented by the class which implements that particular interface.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To create an interface, simply create a new C# script in Unity and add the following lines of code:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-98e95e0 elementor-widget elementor-widget-code-highlight\" data-id=\"98e95e0\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"prismjs-tomorrow copy-to-clipboard \">\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-875e3a8 elementor-widget elementor-widget-text-editor\" data-id=\"875e3a8\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Besides adding the \u201cinterface\u201d keyword in the declaration the coding standard is to put \u201cI\u201d in front of the name of the Interface so that when we implement it in a class we\u2019ll immediately know this class is implementing an Interface.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To implement the Interface in a class, we just add a comma (,) after the MonoBehavior keyword:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6173ed0 elementor-widget elementor-widget-text-editor\" data-id=\"6173ed0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">When you declare that you implement an Interface, you\u2019ll immediately receive a red warning because you need to implement all functions that are declare inside the interface:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-20efc97 elementor-widget elementor-widget-image\" data-id=\"20efc97\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full size-full wp-image-32100\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/e2850cf54559442ed0d62bc9b369b4834e3b8ec867f0a70cac94a57ffa52a636\/85c091bbb9d77f1ed8048216df528e50d55b758e410dc1e07264eaea38e1aa9a.jpeg\" sizes=\"(max-width: 1616px) 100vw, 1616px\" srcset=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/e2850cf54559442ed0d62bc9b369b4834e3b8ec867f0a70cac94a57ffa52a636\/85c091bbb9d77f1ed8048216df528e50d55b758e410dc1e07264eaea38e1aa9a.jpeg 1616w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-1-300x54.jpg 300w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-1-1024x185.jpg 1024w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-1-768x139.jpg 768w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-1-1536x278.jpg 1536w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-1-600x108.jpg 600w\" alt=\"\" width=\"1616\" height=\"292\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-d9c379a elementor-widget elementor-widget-text-editor\" data-id=\"d9c379a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">You can either hover over the Interface name and on the drop-down list implement the Interface using auto-complete:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-a5f475f elementor-widget elementor-widget-image\" data-id=\"a5f475f\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full size-full wp-image-32101\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/e2850cf54559442ed0d62bc9b369b4834e3b8ec867f0a70cac94a57ffa52a636\/927843c9a1d18f66e142dcab9e7823f3f6ee4478eff9ca79160f413e2db1766c.jpeg\" sizes=\"(max-width: 1660px) 100vw, 1660px\" srcset=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/e2850cf54559442ed0d62bc9b369b4834e3b8ec867f0a70cac94a57ffa52a636\/927843c9a1d18f66e142dcab9e7823f3f6ee4478eff9ca79160f413e2db1766c.jpeg 1660w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-2-300x249.jpg 300w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-2-1024x849.jpg 1024w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-2-768x637.jpg 768w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-2-1536x1273.jpg 1536w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-2-600x497.jpg 600w\" alt=\"\" width=\"1660\" height=\"1376\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-b3993e0 elementor-widget elementor-widget-text-editor\" data-id=\"b3993e0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Or you can implement it yourself by adding the functions one by one:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-ae16150 elementor-widget elementor-widget-text-editor\" data-id=\"ae16150\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">As you can see, the signature of the functions declared in an Interface must be the same when you implement them in a class.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If the function has parameters, you need to declare that function with the same parameters.<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-599826b elementor-widget elementor-widget-heading\" data-id=\"599826b\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Implementing Doctor Player\u2019s Movement Using The Interface Function<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-25e370f elementor-widget elementor-widget-text-editor\" data-id=\"25e370f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Let\u2019s use the interface we created to implement the movement behavior of our Doctor game character.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">I\u2019ve already prepared the animations of the character and created a script that will play those animations:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-b9a7fed elementor-widget elementor-widget-text-editor\" data-id=\"b9a7fed\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Also, inside the Doctor script we got a reference to the AnimationController script inside the Awake function:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2eedc0c elementor-widget elementor-widget-text-editor\" data-id=\"2eedc0c\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Since we\u2019re are going to move the Doctor character via transform, we are going to declare two variables that are going to help us with that:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-becfb86 elementor-widget elementor-widget-text-editor\" data-id=\"becfb86\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Now, inside the Walk function we implemented from ICharacterBehavior Interface we are going to add the following lines of code:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-038a1e6 elementor-widget elementor-widget-text-editor\" data-id=\"038a1e6\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">And lastly we are going to call that function inside the Update function so that we can execute it:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-a657060 elementor-widget elementor-widget-text-editor\" data-id=\"a657060\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Now we can run the game and test the Doctor character\u2019s movement:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-e0cd2d9 elementor-widget elementor-widget-heading\" data-id=\"e0cd2d9\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Implementing Doctor Player\u2019s Attack Using The Interface Function<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5d32a86 elementor-widget elementor-widget-text-editor\" data-id=\"5d32a86\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Now that the Doctor character is moving, we can add attack functionality.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Since I\u2019m using a separate game object which has the electricity animation for the Doctor\u2019s gun, I\u2019ve created a variable as a reference to that game object:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-25c6a6f elementor-widget elementor-widget-text-editor\" data-id=\"25c6a6f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">This game object is a child of the Doctor character in the Hierarchy and I\u2019ve dragged and dropped it\u2019s reference inside the script:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-cf197fe elementor-widget elementor-widget-image\" data-id=\"cf197fe\" data-element_type=\"widget\" data-widget_type=\"image.default\">\n<div class=\"elementor-widget-container\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full size-full wp-image-32102\" src=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/e2850cf54559442ed0d62bc9b369b4834e3b8ec867f0a70cac94a57ffa52a636\/5ed8162a17347be1669be2b62111bfa714b98121952832f9ffc40dbcc1ed046b.jpeg\" sizes=\"(max-width: 1640px) 100vw, 1640px\" srcset=\"https:\/\/glossingdress.com\/lib\/img\/all\/551\/e2850cf54559442ed0d62bc9b369b4834e3b8ec867f0a70cac94a57ffa52a636\/5ed8162a17347be1669be2b62111bfa714b98121952832f9ffc40dbcc1ed046b.jpeg 1640w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-3-300x283.jpg 300w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-3-1024x965.jpg 1024w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-3-768x724.jpg 768w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-3-1536x1448.jpg 1536w, https:\/\/awesometuts.com\/wp-content\/uploads\/2022\/11\/C-Interfaces-Image-3-600x566.jpg 600w\" alt=\"\" width=\"1640\" height=\"1546\" \/><\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-0ba2e78 elementor-widget elementor-widget-text-editor\" data-id=\"0ba2e78\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Inside the Attack function we\u2019re going to add the following code:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-58a4bd5 elementor-widget elementor-widget-code-highlight\" data-id=\"58a4bd5\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"prismjs-tomorrow copy-to-clipboard \">\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5228a97 elementor-widget elementor-widget-template\" data-id=\"5228a97\" data-element_type=\"widget\" data-widget_type=\"template.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"elementor-template\">\n<div class=\"elementor elementor-31169\" data-elementor-type=\"section\" data-elementor-id=\"31169\">\n<section class=\"elementor-section elementor-top-section elementor-element elementor-element-7eaceb00 elementor-section-full_width elementor-section-height-default elementor-section-height-default\" data-id=\"7eaceb00\" data-element_type=\"section\" data-settings=\"{\">\n<div class=\"elementor-container elementor-column-gap-default\">\n<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-7542c05a\" data-id=\"7542c05a\" data-element_type=\"column\">\n<div class=\"elementor-widget-wrap elementor-element-populated\">\n<div class=\"elementor-element elementor-element-7a156fc9 elementor-widget elementor-widget-ucaddon_ue_random_image\" data-id=\"7a156fc9\" data-element_type=\"widget\" data-widget_type=\"ucaddon_ue_random_image.default\">\n<div class=\"elementor-widget-container\"><!-- start Random Image - Middle --><\/p>\n<style type=\"text\/css\">\/* widget: Random Image - Middle *\/<\/p>\n<p>#uc_ue_random_image_elementor487645<br \/>\n{<br \/>\n  display:flex;<br \/>\n}<\/p>\n<p>#uc_ue_random_image_elementor487645 .ue-image-item<br \/>\n{<br \/>\n  display:block;<br \/>\n  transition:0.3s;<br \/>\n  width:100%;<br \/>\n}<\/p>\n<p>#uc_ue_random_image_elementor487645 .ue-image-link<br \/>\n{<br \/>\n  display:block;<br \/>\n  color: unset;<br \/>\n}<\/p>\n<\/style>\n<div id=\"uc_ue_random_image_elementor487645\" class=\"ue-random-image\" data-debug=\"false\" data-editor=\"no\">\n<div class=\"ue-image\">\n<div class=\"ue-image-box\">\n<p><img class=\"ue-image-item\" \/><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!-- end Random Image - Middle --><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/section>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-90f0349 elementor-widget elementor-widget-text-editor\" data-id=\"90f0349\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">One reminder, if you\u2019re using this code for your own project, with the current setup the character will be able to move while he\u2019s attacking.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To prevent that you can add a boolean variable:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-786a76a elementor-widget elementor-widget-code-highlight\" data-id=\"786a76a\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"prismjs-tomorrow copy-to-clipboard \">\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-cf1128d elementor-widget elementor-widget-text-editor\" data-id=\"cf1128d\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">And inside the Attack function disable the player from moving while he\u2019s attacking:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-87d7ebf elementor-widget elementor-widget-text-editor\" data-id=\"87d7ebf\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Also, inside the Walk function at the beginning we need add the following condition:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-f77137a elementor-widget elementor-widget-text-editor\" data-id=\"f77137a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">The condition we added on line 38 is preventing the player from moving, but it allows him to change direction so that he can attack enemies on the left and right sides.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This is what it looks like in the preview:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-c1bbf05 elementor-widget elementor-widget-heading\" data-id=\"c1bbf05\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Implementing Hazard Player\u2019s Movement Using The Interface Function<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-6fb9cef elementor-widget elementor-widget-text-editor\" data-id=\"6fb9cef\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Now we\u2019re going to use the same interface to model the behavior of our Hazard character.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Inside the Hazard script, we\u2019ve implemented the interface:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-414d65e elementor-widget elementor-widget-text-editor\" data-id=\"414d65e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">To make the two players different, we\u2019re going to use Rigidbody2D to move the Hazard character so we\u2019re going to declare the following code:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2f8e6b3 elementor-widget elementor-widget-text-editor\" data-id=\"2f8e6b3\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">And of course, inside the Awake function get a reference to the Rigidbody2D component:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2733d1a elementor-widget elementor-widget-code-highlight\" data-id=\"2733d1a\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"prismjs-tomorrow copy-to-clipboard \">\n<\/div>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-5fe19ff elementor-widget elementor-widget-text-editor\" data-id=\"5fe19ff\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">We also have a reference to the AnimatorController class that we used to animate the Doctor character.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Inside the Walk function, we are going to add the following code:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-32fabb9 elementor-widget elementor-widget-text-editor\" data-id=\"32fabb9\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">And inside the Update we\u2019ll run the Walk function:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-e67ff14 elementor-widget elementor-widget-text-editor\" data-id=\"e67ff14\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">When we play the game this is how it looks like:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-f3d6f62 elementor-widget elementor-widget-heading\" data-id=\"f3d6f62\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Implementing Hazard Player\u2019s Attack Using The Interface Function<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-8b05494 elementor-widget elementor-widget-text-editor\" data-id=\"8b05494\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Since the Hazard character attack animation is different from the Doctor character animation, and we\u2019re using a trigger as a parameter to play that animation, we added one more function inside the AnimationController:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2c1cefc elementor-widget elementor-widget-text-editor\" data-id=\"2c1cefc\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Now inside the Attack function we\u2019re going to add the following lines:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-52f2b0e elementor-widget elementor-widget-text-editor\" data-id=\"52f2b0e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">This is how it looks like in the preview:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-ef88c9f elementor-widget elementor-widget-heading\" data-id=\"ef88c9f\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Why Is This Approach Better Than What All Other Tutorials And Courses Are Teaching You?<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-c92b09e elementor-widget elementor-widget-text-editor\" data-id=\"c92b09e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">As you saw, we created the same functionality for both characters using the Interface structure.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We even implemented the Walk functions differently by using transform to move the Doctor character and Rigidbody2D to move the Hazard character.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">But why is this approach better than if we just implemented characters movement inside their appropriate scripts?<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This way is more simple because we model the behavior of all characters inside the Interface.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When you create a game where you have a common behavior for characters, enemies, or any other game object, you can model the behavior inside the Interface and implement that behavior differently for each game object.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">And if somewhere down the line you decide to add new features in your characters, you first model that feature inside the Interface, and then you implement it in every character.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can also invoke Interface functions on game objects that have implemented that interface without getting a reference to that game object.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, we can implement the damage function inside the Interface:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-4094759 elementor-widget elementor-widget-text-editor\" data-id=\"4094759\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Inside the game object that has implemented the Interface we implement the function:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-aeb7bc0 elementor-widget elementor-widget-text-editor\" data-id=\"aeb7bc0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">And when we want to deal damage to that game object we can do that by calling the Interface:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-26ba006 elementor-widget elementor-widget-text-editor\" data-id=\"26ba006\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">In the example above, instead of checking if we have the right game object, we\u2019ll invoke the function from the Interface and in turn, it will call this code:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-c8403fa elementor-widget elementor-widget-text-editor\" data-id=\"c8403fa\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Or whichever code we implemented in the particular class where we inherited the Interface.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This allows us to have more control over the structure of our game, and we can easily add new features, improve the current ones, or remove old features and all that without breaking our game or introducing hundreds of bugs.<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-006c08d elementor-widget elementor-widget-heading\" data-id=\"006c08d\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Modeling Single Behaviors With Interfaces<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-e7eec96 elementor-widget elementor-widget-text-editor\" data-id=\"e7eec96\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">We can even model single behaviors using an Interface. Using our damage example we can create an IDamageable Interface like this:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-00bf43d elementor-widget elementor-widget-text-editor\" data-id=\"00bf43d\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">Now we can deal damage to every game object which implements this Interface and we can do that by calling the Interface itself like we did in the example above:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-2f5a9d1 elementor-widget elementor-widget-text-editor\" data-id=\"2f5a9d1\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">And there\u2019s no limit to how many behaviors we can model using the Interface approach and we will not have to get references to different game objects every time we need them, instead we can just use the Interface to invoke their functionality.<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-f0d9a45 elementor-widget elementor-widget-heading\" data-id=\"f0d9a45\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n<div class=\"elementor-widget-container\">\n<h3 class=\"elementor-heading-title elementor-size-default\">Where To Go From Here<\/h3>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-0873c5c elementor-widget elementor-widget-text-editor\" data-id=\"0873c5c\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n<div class=\"elementor-widget-container\">\n<p><span style=\"font-weight: 400;\">You can check out some posts we made, like how to use a Coroutine in Unity or how to implement Delegates and Events in your game.<\/span><\/p>\n<p>Unity Coroutines \u2013 What Are They And How To Use Them<\/p>\n<p>Delegates And Events In Unity \u2013 What Are They And How To Use Them<\/p>\n<p>Or you can check out some of the other blog posts by clicking on the link below:<\/p>\n<p>Awesome Tuts Blog<\/p>\n<\/div>\n<\/div>\n<div class=\"elementor-element elementor-element-fec12d3 elementor-widget elementor-widget-template\" data-id=\"fec12d3\" data-element_type=\"widget\" data-widget_type=\"template.default\">\n<div class=\"elementor-widget-container\">\n<div class=\"elementor-template\">\n<div class=\"elementor elementor-31169\" data-elementor-type=\"section\" data-elementor-id=\"31169\">\n<section class=\"elementor-section elementor-top-section elementor-element elementor-element-7eaceb00 elementor-section-full_width elementor-section-height-default elementor-section-height-default\" data-id=\"7eaceb00\" data-element_type=\"section\" data-settings=\"{\">\n<div class=\"elementor-container elementor-column-gap-default\">\n<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-7542c05a\" data-id=\"7542c05a\" data-element_type=\"column\">\n<div class=\"elementor-widget-wrap elementor-element-populated\">\n<div class=\"elementor-element elementor-element-7a156fc9 elementor-widget elementor-widget-ucaddon_ue_random_image\" data-id=\"7a156fc9\" data-element_type=\"widget\" data-widget_type=\"ucaddon_ue_random_image.default\">\n<div class=\"elementor-widget-container\"><!-- start Random Image - Middle --><\/p>\n<style type=\"text\/css\">\/* widget: Random Image - Middle *\/<\/p>\n<p>#uc_ue_random_image_elementor491192<br \/>\n{<br \/>\n  display:flex;<br \/>\n}<\/p>\n<p>#uc_ue_random_image_elementor491192 .ue-image-item<br \/>\n{<br \/>\n  display:block;<br \/>\n  transition:0.3s;<br \/>\n  width:100%;<br \/>\n}<\/p>\n<p>#uc_ue_random_image_elementor491192 .ue-image-link<br \/>\n{<br \/>\n  display:block;<br \/>\n  color: unset;<br \/>\n}<\/p>\n<\/style>\n<div id=\"uc_ue_random_image_elementor491192\" class=\"ue-random-image\" data-debug=\"false\" data-editor=\"no\">\n<div class=\"ue-image\">\n<div class=\"ue-image-box\">\n<p><img class=\"ue-image-item\" \/><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!-- end Random Image - Middle --><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/section>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/section>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>When creating a game in Unity the most important thing is to structure your project in a way that you can expand it later by adding new updates to the game. This is done from the beginning when you start creating parts of your game, not like what you see on all YouTube tutorials and<\/p>\n","protected":false},"author":1,"featured_media":965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-845","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/posts\/845"}],"collection":[{"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=845"}],"version-history":[{"count":2,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/posts\/845\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/posts\/845\/revisions\/966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=\/wp\/v2\/media\/965"}],"wp:attachment":[{"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/globalgoodplay.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}