{"id":31763,"date":"2024-11-01T09:02:38","date_gmt":"2024-11-01T09:02:38","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31763"},"modified":"2024-11-01T11:34:50","modified_gmt":"2024-11-01T11:34:50","slug":"basic-unity-course-bullet-firing","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31763\/","title":{"rendered":"Basic Unity Course: Bullet Firing"},"content":{"rendered":"<div class=\"post\">\n<p>In this tutorial, we will implement a simple bullet shooting system using Unity. This tutorial is based on a basic understanding of Unity and will be conducted step by step so that beginners can easily follow along. By completing this tutorial, you will lay the foundation to apply the shooting functionality in 2D or 3D games.<\/p>\n<h2>Table of Contents<\/h2>\n<ul>\n<li><a href=\"#setting-up\">1. Setting Up<\/a><\/li>\n<li><a href=\"#creating-project\">2. Creating a Unity Project<\/a><\/li>\n<li><a href=\"#adding-sprites\">3. Adding and Setting Up Sprites<\/a><\/li>\n<li><a href=\"#bullet-script\">4. Creating the Bullet Script<\/a><\/li>\n<li><a href=\"#gun-control\">5. Creating the Gun Control Script<\/a><\/li>\n<li><a href=\"#testing\">6. Testing and Debugging<\/a><\/li>\n<li><a href=\"#conclusion\">7. Conclusion<\/a><\/li>\n<\/ul>\n<h2 id=\"setting-up\">1. Setting Up<\/h2>\n<p>To effectively use Unity, appropriate setup is necessary. Download and install the latest version of Unity from the Unity Hub. Use the package manager to install the packages needed for the current project. In particular, the <strong>Physics<\/strong> and <strong>2D Physics<\/strong> packages are required.<\/p>\n<h2 id=\"creating-project\">2. Creating a Unity Project<\/h2>\n<p>Open Unity Hub and create a new project. Choose between 2D or 3D as you prefer. It is recommended to proceed in 3D for this tutorial. Set the project name to &#8216;BulletShooter&#8217;, specify the path, and then click the &#8216;Create&#8217; button.<\/p>\n<h2 id=\"adding-sprites\">3. Adding and Setting Up Sprites<\/h2>\n<p>You need to add the sprites for the bullet and the gun. Prepare the sprite images and drag them into the &#8216;Assets&#8217; folder to add them. Then, adjust the appearance and size of the images using the <strong>Inspector<\/strong> panel and <strong>Sprite Renderer<\/strong> for each sprite.<\/p>\n<h3>3.1. Creating Bullet Sprite<\/h3>\n<p>After selecting the bullet sprite, click the <strong>Add Component<\/strong> button to add <strong>Rigidbody<\/strong> and <strong>Collider<\/strong>. By disabling the gravity of the Rigidbody, the bullet will be shot in a straight line.<\/p>\n<h3>3.2. Creating Gun Sprite<\/h3>\n<p>Similarly, add Rigidbody and Collider to the gun&#8217;s sprite. Position the gun appropriately to adjust the firing direction of the bullet.<\/p>\n<h2 id=\"bullet-script\">4. Creating the Bullet Script<\/h2>\n<p>Now let&#8217;s write a script to define how the bullet is fired. Right-click in the empty space of the &#8216;Assets&#8217; folder, select <strong>Create &gt; C# Script<\/strong>, and create a script named &#8216;Bullet&#8217;.<\/p>\n<h3>4.1. Writing Bullet.cs Code<\/h3>\n<pre>\n    <code>\n    using UnityEngine;\n    \n    public class Bullet : MonoBehaviour\n    {\n        public float speed = 20f;\n\n        void Start()\n        {\n            Rigidbody rb = GetComponent<rigidbody>();\n            rb.velocity = transform.forward * speed;\n        }\n\n        void OnTriggerEnter(Collider other)\n        {\n            if (other.CompareTag(\"Enemy\"))\n            {\n                Destroy(other.gameObject);\n                Destroy(gameObject);\n            }\n        }\n    }\n    <\/code>\n<\/pre>\n<p>The above code allows the bullet to move forward at the speed set through the Rigidbody component upon firing. It is implemented to destroy the bullet and the enemy object upon collision.<\/p>\n<h2 id=\"gun-control\">5. Creating the Gun Control Script<\/h2>\n<p>Now it&#8217;s time to add the functionality for the gun to shoot bullets. Create a new script called &#8216;Gun&#8217; and write the following code.<\/p>\n<h3>5.1. Writing Gun.cs Code<\/h3>\n<pre>\n    <code>\n    using UnityEngine;\n\n    public class Gun : MonoBehaviour\n    {\n        public GameObject bulletPrefab;\n        public Transform firePoint;\n        public float fireRate = 1f;\n        private float nextFireTime = 0f;\n\n        void Update()\n        {\n            if (Input.GetButton(\"Fire1\") &amp;&amp; Time.time &gt;= nextFireTime)\n            {\n                nextFireTime = Time.time + 1f \/ fireRate;\n                Shoot();\n            }\n        }\n\n        void Shoot()\n        {\n            Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);\n        }\n    }\n    <\/code>\n<\/pre>\n<p>This script shoots a bullet when the user presses the mouse button. It creates an instance at the <strong>firePoint<\/strong> location to enable the bullet&#8217;s shooting mechanism.<\/p>\n<h2 id=\"testing\">6. Testing and Debugging<\/h2>\n<p>Now that everything is set up, you need to test whether the code works without issues. Click the play button in the Unity Editor to run the game. Press the mouse button to check if the bullets are being fired. Also, confirm that bullets are destroyed upon contact with enemies.<\/p>\n<h2 id=\"conclusion\">7. Conclusion<\/h2>\n<p>In this tutorial, we built a simple bullet shooting system using Unity. We covered sprite setup, using Rigidbody, and implementing the shooting functionality through scripts. Now you can use this foundational knowledge to advance into more complex and interesting game development. Unity has many features, so keep practicing and improving.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will implement a simple bullet shooting system using Unity. This tutorial is based on a basic understanding of Unity and will be conducted step by step so that beginners can easily follow along. By completing this tutorial, you will lay the foundation to apply the shooting functionality in 2D or 3D &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31763\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Basic Unity Course: Bullet Firing&#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-31763","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: Bullet Firing - \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\/31763\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basic Unity Course: Bullet Firing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we will implement a simple bullet shooting system using Unity. This tutorial is based on a basic understanding of Unity and will be conducted step by step so that beginners can easily follow along. By completing this tutorial, you will lay the foundation to apply the shooting functionality in 2D or 3D &hellip; \ub354 \ubcf4\uae30 &quot;Basic Unity Course: Bullet Firing&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31763\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:02:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:34:50+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\/31763\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31763\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Basic Unity Course: Bullet Firing\",\"datePublished\":\"2024-11-01T09:02:38+00:00\",\"dateModified\":\"2024-11-01T11:34:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31763\/\"},\"wordCount\":525,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31763\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31763\/\",\"name\":\"Basic Unity Course: Bullet Firing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:02:38+00:00\",\"dateModified\":\"2024-11-01T11:34:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31763\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31763\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31763\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basic Unity Course: Bullet Firing\"}]},{\"@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: Bullet Firing - \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\/31763\/","og_locale":"ko_KR","og_type":"article","og_title":"Basic Unity Course: Bullet Firing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this tutorial, we will implement a simple bullet shooting system using Unity. This tutorial is based on a basic understanding of Unity and will be conducted step by step so that beginners can easily follow along. By completing this tutorial, you will lay the foundation to apply the shooting functionality in 2D or 3D &hellip; \ub354 \ubcf4\uae30 \"Basic Unity Course: Bullet Firing\"","og_url":"https:\/\/atmokpo.com\/w\/31763\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:02:38+00:00","article_modified_time":"2024-11-01T11:34:50+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\/31763\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31763\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Basic Unity Course: Bullet Firing","datePublished":"2024-11-01T09:02:38+00:00","dateModified":"2024-11-01T11:34:50+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31763\/"},"wordCount":525,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31763\/","url":"https:\/\/atmokpo.com\/w\/31763\/","name":"Basic Unity Course: Bullet Firing - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:02:38+00:00","dateModified":"2024-11-01T11:34:50+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31763\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31763\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31763\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Basic Unity Course: Bullet Firing"}]},{"@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\/31763","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=31763"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31763\/revisions"}],"predecessor-version":[{"id":31764,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31763\/revisions\/31764"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}