{"id":32069,"date":"2024-11-01T09:05:24","date_gmt":"2024-11-01T09:05:24","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32069"},"modified":"2024-11-01T11:33:27","modified_gmt":"2024-11-01T11:33:27","slug":"basic-unity-course-player-synchronization-and-controlling-only-my-player-character","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32069\/","title":{"rendered":"Basic Unity Course: Player Synchronization and Controlling Only My Player Character"},"content":{"rendered":"<p><body><\/p>\n<p>This course will cover how to synchronize players in multiplayer games using Unity, allowing each client to control only their player character. The course will start with the basics of network programming, explain Unity&#8217;s networking system, and discuss the client-server architecture. Additionally, we will learn how to manage and synchronize the input of each player.<\/p>\n<h2>1. Basic Concepts of Unity Networking<\/h2>\n<p>Unity provides powerful features that make it easy to develop multiplayer games. To understand the network system, you need to grasp a few basic concepts.<\/p>\n<h3>1.1 Client-Server Architecture<\/h3>\n<p>The client-server architecture is a crucial concept in network games. The client refers to the instance of the game that a player can use, while the server is the central system that manages the state of all clients.<\/p>\n<ul>\n<li><strong>Server:<\/strong> Manages all game progress and sends information to clients.<\/li>\n<li><strong>Client:<\/strong> Processes player inputs and progresses the game based on information received from the server.<\/li>\n<\/ul>\n<h3>1.2 Network Synchronization<\/h3>\n<p>It is essential to synchronize the states of player characters or objects in the game. That is, the same information must be provided to all clients to achieve smooth gameplay.<\/p>\n<h2>2. Installing Unity and Creating a Project<\/h2>\n<p>First, you need to install Unity and create a new project. Follow these steps:<\/p>\n<ol>\n<li>Install Unity Hub.<\/li>\n<li>Download and install the desired version of Unity.<\/li>\n<li>Click the &#8216;New Project&#8217; button in Unity Hub and select either a 3D or 2D project.<\/li>\n<li>Choose a name and a save location for the project, then click the &#8216;Create&#8217; button.<\/li>\n<\/ol>\n<h2>3. Setting Up the Networking Package<\/h2>\n<p>There are various networking libraries available in Unity, but in this course, we will use Unity\u2019s <strong>MLAPI<\/strong> (Mid-Level API). Follow the steps below to install MLAPI.<\/p>\n<ol>\n<li>Open the Package Manager (Window > Package Manager) and click the &#8216;+&#8217; button in the top left corner.<\/li>\n<li>Select &#8216;Git URL&#8217; and enter <code>https:\/\/github.com\/Unity-Technologies\/Mirror.git<\/code>, then install it.<\/li>\n<\/ol>\n<h2>4. Basic Network Settings<\/h2>\n<p>After installing the networking package, you will need to set up the basic network settings. Follow these steps:<\/p>\n<ol>\n<li>Right-click in the Hierarchy view to add a <strong>NetworkManager<\/strong> object.<\/li>\n<li>Add the scenes you want to use in the NetworkManager&#8217;s settings menu.<\/li>\n<li>Add NetworkManagerHUD to set up the basic UI.<\/li>\n<\/ol>\n<h3>4.1 Configuring the Network Manager<\/h3>\n<p>Set up the NetworkManager to manage servers and clients. To achieve basic functionality, you need to be aware of the following settings:<\/p>\n<ul>\n<li>Maximum Connections: Set the maximum number of clients that can connect.<\/li>\n<li>Network Port: Set the port number that the server will use.<\/li>\n<\/ul>\n<h2>5. Player Settings<\/h2>\n<p>You need to set up each player&#8217;s character. Since the player character is directly controlled and needs to be synchronized with other clients, it should be created as a prefab.<\/p>\n<h3>5.1 Creating a Player Character Prefab<\/h3>\n<ol>\n<li>Create a player character as a new 3D object (e.g., <strong>Cylinder<\/strong>).<\/li>\n<li>Add a <strong>Rigidbody<\/strong> component to the game object for movement and rotation.<\/li>\n<li>Create the above object as a prefab and save it in the Resources folder.<\/li>\n<\/ol>\n<h2>6. Writing the Player Control Script<\/h2>\n<p>Now, let\u2019s write a script that will control the player character. Refer to the code below to implement the <strong>PlayerController.cs<\/strong> script.<\/p>\n<pre><code>using UnityEngine;\nusing Mirror;\n\npublic class PlayerController : NetworkBehaviour\n{\n    public float speed = 5f;\n\n    void Update()\n    {\n        if (!isLocalPlayer)\n            return;\n\n        float moveHorizontal = Input.GetAxis(\"Horizontal\") * speed * Time.deltaTime;\n        float moveVertical = Input.GetAxis(\"Vertical\") * speed * Time.deltaTime;\n\n        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);\n        transform.Translate(movement);\n    }\n}\n<\/code><\/pre>\n<h3>6.1 Script Explanation<\/h3>\n<p>The above script constructs a structure for controlling the player using basic WASD\/arrow key inputs. It checks <code>isLocalPlayer<\/code> to ensure that only the player character of the current client can move.<\/p>\n<h2>7. Synchronizing Players Over the Network<\/h2>\n<p>The next step is to ensure that all player characters communicate with the server and synchronize. We will use <strong>SyncVar<\/strong> and <strong>Command<\/strong> to synchronize states over the network.<\/p>\n<h3>7.1 Setting Up SyncVar<\/h3>\n<p>SyncVar is used to synchronize variables over the network and automatically reflects any changes that occur on the server to the clients.<\/p>\n<pre><code>public class PlayerController : NetworkBehaviour\n{\n    [SyncVar]\n    public Vector3 position;\n\n    void Update()\n    {\n        if (!isLocalPlayer)\n            return;\n\n        float moveHorizontal = Input.GetAxis(\"Horizontal\") * speed * Time.deltaTime;\n        float moveVertical = Input.GetAxis(\"Vertical\") * speed * Time.deltaTime;\n\n        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);\n        position += movement; \/\/ Update the local position\n        CmdUpdatePosition(position); \/\/ Command to update the server\n    }\n\n    [Command]\n    void CmdUpdatePosition(Vector3 newPosition)\n    {\n        position = newPosition; \/\/ Update the server's position\n    }\n}\n<\/code><\/pre>\n<h2>8. Conclusion<\/h2>\n<p>In this course, we learned how to synchronize players in Unity and allow each player to control only their character. By setting up a basic network environment and scripting the players, we established the foundation for multiplayer games in Unity.<\/p>\n<p>In future courses, we will add more features and increase the complexity of the game, covering more advanced topics. If you have any questions or feedback, please leave a comment!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This course will cover how to synchronize players in multiplayer games using Unity, allowing each client to control only their player character. The course will start with the basics of network programming, explain Unity&#8217;s networking system, and discuss the client-server architecture. Additionally, we will learn how to manage and synchronize the input of each player. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32069\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Basic Unity Course: Player Synchronization and Controlling Only My Player Character&#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-32069","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>Basic Unity Course: Player Synchronization and Controlling Only My Player Character - \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\/32069\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basic Unity Course: Player Synchronization and Controlling Only My Player Character - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"This course will cover how to synchronize players in multiplayer games using Unity, allowing each client to control only their player character. The course will start with the basics of network programming, explain Unity&#8217;s networking system, and discuss the client-server architecture. Additionally, we will learn how to manage and synchronize the input of each player. &hellip; \ub354 \ubcf4\uae30 &quot;Basic Unity Course: Player Synchronization and Controlling Only My Player Character&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32069\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:05:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:33: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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32069\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32069\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Basic Unity Course: Player Synchronization and Controlling Only My Player Character\",\"datePublished\":\"2024-11-01T09:05:24+00:00\",\"dateModified\":\"2024-11-01T11:33:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32069\/\"},\"wordCount\":678,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32069\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32069\/\",\"name\":\"Basic Unity Course: Player Synchronization and Controlling Only My Player Character - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:05:24+00:00\",\"dateModified\":\"2024-11-01T11:33:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32069\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32069\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32069\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basic Unity Course: Player Synchronization and Controlling Only My Player Character\"}]},{\"@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":"Basic Unity Course: Player Synchronization and Controlling Only My Player Character - \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\/32069\/","og_locale":"ko_KR","og_type":"article","og_title":"Basic Unity Course: Player Synchronization and Controlling Only My Player Character - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"This course will cover how to synchronize players in multiplayer games using Unity, allowing each client to control only their player character. The course will start with the basics of network programming, explain Unity&#8217;s networking system, and discuss the client-server architecture. Additionally, we will learn how to manage and synchronize the input of each player. &hellip; \ub354 \ubcf4\uae30 \"Basic Unity Course: Player Synchronization and Controlling Only My Player Character\"","og_url":"https:\/\/atmokpo.com\/w\/32069\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:05:24+00:00","article_modified_time":"2024-11-01T11:33: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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32069\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32069\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Basic Unity Course: Player Synchronization and Controlling Only My Player Character","datePublished":"2024-11-01T09:05:24+00:00","dateModified":"2024-11-01T11:33:27+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32069\/"},"wordCount":678,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32069\/","url":"https:\/\/atmokpo.com\/w\/32069\/","name":"Basic Unity Course: Player Synchronization and Controlling Only My Player Character - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:05:24+00:00","dateModified":"2024-11-01T11:33:27+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32069\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32069\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32069\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Basic Unity Course: Player Synchronization and Controlling Only My Player Character"}]},{"@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\/32069","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=32069"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32069\/revisions"}],"predecessor-version":[{"id":32070,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32069\/revisions\/32070"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}