Hello! In this course, we will create a simple shooting game using Unity. The main focus of our implementation will be on the “shooting bullets” feature. We will gradually learn the necessary functions and aim to complete a simple game at the end.
Table of Contents
- Installing Unity and Basic Setup
- Creating a Project
- Setting Up a 2D Game Environment
- Creating a Bullet Prefab
- Writing the Character and Bullet Shooting Script
- Configuring and Testing Game Objects
- Conclusion
1. Installing Unity and Basic Setup
To install Unity, first download Unity Hub from the Unity website. After installing Unity Hub, install the desired version of the Unity Editor. Once the installation is complete, you can run Unity Hub to create a new project.
2. Creating a Project
In Unity Hub, click the New Project button and select the 2D template. Name the project ShootingGame and set the save location, then click the Create button to create the project.
3. Setting Up a 2D Game Environment
Once the project is created, the Unity Editor will open. Here, we will place game objects and set up the camera and background.
3.1 Setting Up the Camera
Select the main camera and adjust the viewport to an appropriate size. In a 2D game, it’s common to set the camera’s Projection to Orthographic.
3.2 Adding a Background
Add an image to be used as the background in the Assets folder. Then drag it to the scene to place it. Set the z-axis value of the image lower than that of the camera to ensure the background is visible.
4. Creating a Bullet Prefab
We will create a simple sprite representing the bullet. Add the bullet sprite to the Assets folder and place it into the scene as a 2D object. After adjusting the size and properties of the bullet, drag this object into the Prefabs folder to save it as a prefab.
4.1 Setting Bullet Properties
Select the prefab and add a Rigidbody2D component. This component gives physical properties so that the bullet can move naturally. Set the Gravity Scale to 0 so that it is not affected by gravity.
4.2 Adding a Collider
Add a CircleCollider2D to the bullet prefab to set up the collider. This collider allows the bullet to detect collisions with other objects.
5. Writing the Character and Bullet Shooting Script
Now we need to write a script to control the shooting of the bullet. Let’s create the character and add the shooting functionality.
5.1 Creating the Character
Prepare a sprite to be used as the character and add it to the scene in the editor. Also, add a Rigidbody2D component to the character.
5.2 Writing the Script
Create the script for the character. Below is a basic C# script example to shoot bullets.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject bulletPrefab; // Bullet prefab
public float bulletSpeed = 20f; // Bullet speed
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) // When the space bar is pressed
{
Shoot();
}
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
Rigidbody2D rb = bullet.GetComponent();
rb.velocity = transform.up * bulletSpeed; // Set the bullet's speed in its direction
}
}
5.3 Connecting the Script
Add the written script to the character object and set the bulletPrefab variable to the bullet prefab in the inspector window.
6. Configuring and Testing Game Objects
All components are prepared, so let’s test if the game works well.
6.1 Running the Test
Click the play button to run the game. Check if the bullet is fired when the character presses the space bar. If needed, you can adjust the speed or direction to achieve the desired outcome.
6.2 Testing Bullet Collisions
It’s also important to add handling for when the bullet collides with other objects. For example, you could make the enemy disappear when hit by a bullet. To do this, write a script for the enemy object to check for collisions with the bullet.
using UnityEngine;
public class Enemy : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Bullet"))
{
Destroy(gameObject); // Destroy the enemy object
}
}
}
Conclusion
In this course, we implemented a simple shooting mechanism using Unity. By learning the basic functions of shooting bullets and handling collisions, we have laid the groundwork for game development and a stepping stone for more advanced programming. I hope you continue to create various games with Unity!
This concludes the Unity Basics Course – Shooting 1: A Shooting Tutorial Using Bullets. If you have any questions or comments, please leave them below. Thank you!