Basic Unity Course: Bullet Creation

1. Introduction

In this course, we will cover how to create a simple bullet using the Unity game engine. Bullets are a very important element in game development, and we will address how bullets work, their firing mechanisms, and collision handling. Through this course, you will gain an understanding of the fundamental concepts of Unity and get hands-on experience with C# scripting.

2. Required Tools and Environment Setup

2.1. Installing Unity

To install Unity, visit the official Unity website and download the latest version of Unity Hub. After launching Unity Hub, you can download and install the desired version of Unity.

2.2. Creating a New Project

In Unity Hub, click the “New” button to create a new 3D project. Set the project’s name to “BulletShooter” and create it.

3. Creating a Basic Bullet Object

3.1. Adding a 3D Object

In the project window, right-click and select 3D Object > Sphere. The created sphere object will represent the bullet. With the object selected, adjust the Scale value in the inspector window to (0.2, 0.2, 0.2) to set its size.

3.2. Creating a Bullet Material

In the project window, right-click and select Materials > New Material. Name the new material “BulletMaterial”, select a color, and drag and drop it onto the sphere to apply the material.

3.3. Adding a Rigidbody Component to the Bullet Object

To allow the bullet to move physically, click Add Component in the property window and search for Rigidbody to add it. This component enables gravity and physical interactions.

4. Writing the Bullet Firing Script

4.1. Creating the Script

In the project window, create a Scripts folder and then create a script named Bullet.cs inside it. This script will define the behavior of the bullet.

4.2. Writing the Script Code

Open the Bullet.cs file and enter the following code:


using UnityEngine;

public class Bullet : MonoBehaviour
{
    public float speed = 20f;
    public float lifetime = 2f;

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

        Destroy(gameObject, lifetime);
    }
}
    

The code above sets the speed and lifetime of the bullet when it is fired. The Start method is called when the script is activated, setting the bullet’s speed through the Rigidbody component, and the bullet is destroyed after a certain amount of time.

5. Creating the Player and Bullet Firing System

5.1. Creating the Player Object

To add a player that can fire bullets, select 3D Object > Cube to create the player object. Adjust the player’s size and position to create an appropriate shape.

5.2. Writing the Player Script

To add the ability for the player to fire bullets, write a script named PlayerController.cs. The content of this script is as follows:


using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public GameObject bulletPrefab;
    public Transform bulletSpawnPoint;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Fire();
        }
    }

    void Fire()
    {
        Instantiate(bulletPrefab, bulletSpawnPoint.position, bulletSpawnPoint.rotation);
    }
}
    

The above script calls the Fire method to instantiate a bullet when the player presses the “Fire1” input (typically the left mouse button).

6. Setting Up the Spawn Point

To set the position where bullets are fired from, create an empty game object as a child of the player object. Name this empty game object “BulletSpawnPoint”. Adjust the position of this object to match the player’s muzzle.

6.1. Connecting the Script

Select the player object and drag the PlayerController script into the inspector to add it. Then, link the bullet prefab to the Bullet Prefab field and drag the newly created empty game object to link it to the Bullet Spawn Point.

7. Final Check and Testing

7.1. Creating the Bullet Prefab

To create a bullet prefab, drag the bullet object from the project window and drop it into the Prefabs folder. You can now delete the original object.

7.2. Running the Game

Run the game to test if the player can fire bullets. By clicking the left mouse button, you should see the bullets being instantiated and fired.

8. Implementing Additional Features

8.1. Implementing Collision Handling

To handle when the bullet collides with other objects, simply add the OnTriggerEnter method to the Bullet.cs script:


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

This code removes the enemy and destroys the bullet when the bullet collides with an enemy.

8.2. Fine-tuning and Additional Elements

To make the game more complete, you can add bullet trajectories, firing animations, sound effects, and more. Explore various Unity features related to these aspects as you expand the project.

9. Conclusion

In this tutorial, we learned how to implement a basic bullet using Unity. It was an important process for laying the foundation in game development, and I hope you will expand your game by adding more complex features in the future. Experimenting and learning more will help you create various game elements.