Basic Unity Course: Bullet Firing

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.

Table of Contents

1. Setting Up

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 Physics and 2D Physics packages are required.

2. Creating a Unity Project

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 ‘BulletShooter’, specify the path, and then click the ‘Create’ button.

3. Adding and Setting Up Sprites

You need to add the sprites for the bullet and the gun. Prepare the sprite images and drag them into the ‘Assets’ folder to add them. Then, adjust the appearance and size of the images using the Inspector panel and Sprite Renderer for each sprite.

3.1. Creating Bullet Sprite

After selecting the bullet sprite, click the Add Component button to add Rigidbody and Collider. By disabling the gravity of the Rigidbody, the bullet will be shot in a straight line.

3.2. Creating Gun Sprite

Similarly, add Rigidbody and Collider to the gun’s sprite. Position the gun appropriately to adjust the firing direction of the bullet.

4. Creating the Bullet Script

Now let’s write a script to define how the bullet is fired. Right-click in the empty space of the ‘Assets’ folder, select Create > C# Script, and create a script named ‘Bullet’.

4.1. Writing Bullet.cs Code

    
    using UnityEngine;
    
    public class Bullet : MonoBehaviour
    {
        public float speed = 20f;

        void Start()
        {
            Rigidbody rb = GetComponent();
            rb.velocity = transform.forward * speed;
        }

        void OnTriggerEnter(Collider other)
        {
            if (other.CompareTag("Enemy"))
            {
                Destroy(other.gameObject);
                Destroy(gameObject);
            }
        }
    }
    

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.

5. Creating the Gun Control Script

Now it’s time to add the functionality for the gun to shoot bullets. Create a new script called ‘Gun’ and write the following code.

5.1. Writing Gun.cs Code

    
    using UnityEngine;

    public class Gun : MonoBehaviour
    {
        public GameObject bulletPrefab;
        public Transform firePoint;
        public float fireRate = 1f;
        private float nextFireTime = 0f;

        void Update()
        {
            if (Input.GetButton("Fire1") && Time.time >= nextFireTime)
            {
                nextFireTime = Time.time + 1f / fireRate;
                Shoot();
            }
        }

        void Shoot()
        {
            Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        }
    }
    

This script shoots a bullet when the user presses the mouse button. It creates an instance at the firePoint location to enable the bullet’s shooting mechanism.

6. Testing and Debugging

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.

7. Conclusion

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.