First Encounter with a Small Demo Made in Unity
-
Setting Up the Spaceship and Scene
Scene Layout: Place the lights in appropriate positions, pull the camera above the lights, create a new quad in the scene as a background, apply a material texture to it, drag the spaceship player into the scene, adjust its position, and add a fire effect at the tail of the spaceship. -
Writing a Flight Script for the Spaceship
Player.cs:
Drag the player.cs script onto the spaceship in Unity, making it a child object of the spaceship, so that the spaceship can be controlled with the up, down, left, and right keys on the keyboard.
- Creating Bullet Flight and Prefab
Create a new game empty in the hierarchy named bolt, create a new quad as a child object of bolt, apply a material to it, adjust the bullet model's position below the spaceship, and create a flight script bolt_move.cs for the bullet:
Similarly, drag the script onto the bullet, add a rigidbody component and a capsule collider to the bullet:
Create a prefab for the bullet and place it in _prefab:
Just drag the bolt from the hierarchy to _prefab in the assets.
- Bullet Firing
Create a bullet firing script and place it in the player:
Click on the player and complete the settings in the inspector:
Run it, and the bullets will fire from below the airplane.
- Bullet Destruction and Collision Detection
Create a new cube named boundary, adjust its size and position to surround the spaceship.
Create a bullet destruction script destbyboundary.cs:
Drag it into the boundary:
The relevant settings are as follows.
Run it, and the bullets will be destroyed when they fly out of the boundary.
- Adding Meteor Movement and Destruction
Create a new game object named Asterial, drag a meteor model as a child object, adjust its position in the scene, and create a movement script as_move.cs for it:
Add a rigidbody component and a capsule collider to it.
Also, create a prefab for it, which will appear automatically later.
- Adding Tag
Add the following code to the as_move.cs script:
// Asteroid rigidbody and collider trigger, boundary has a collider trigger, it will naturally respond when the trigger enters.
Make property response modifications here.
- Explosion of Meteor and Spaceship
Add the following code to as_move.cs:
Set as shown in the figure.
After running, the shooting meteor and explosion effects after collision will appear.
- Batch Generation of Meteors:
Here we need to use a coroutine method:
Create a new game object named gamecontroller, and create a script gamecontroller.cs:
Set the required parameters according to the scene.
Run it, and the meteors can be generated randomly.
- Creating a Data Transfer Between Different Scripts, Creating a Game Lifecycle: Record Scores, Game Over, and Restart
Add the following code to as_move.cs:
Add the following code to gamecontroller.cs:
Set up the scene as follows:


Thus, a small space spaceship game is basically completed.

Run the game:
