First Encounter with a Small Demo Made in Unity
-
Setting up the spaceship and other scenes
Scene layout: Place the lights in appropriate positions, pull the camera above the lights, create a new quad in the scene as the 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 using 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 also 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 into the _prefab in 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 spaceship.
- 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 onto the boundary:
The relevant settings are as follows:
Run it, and the bullets will be destroyed when they fly out of the boundary.
- Adding asteroid movement and destruction
Create a new game object named Asterial, drag an asteroid model as a child object, adjust its position in the scene, and create a movement script as_move.cs for it:
Also add a Rigidbody component and a capsule collider to it.
At the same time, create a prefab for it, which will appear automatically later.
- Adding tag
Add the following code in the as_move.cs script:
// Asteroid rigidbody and collider trigger, boundary has a collider trigger, when trigger enter it will naturally respond.
Make property response modifications here.
- Asteroid and spaceship explosion
Add the following code in as_move.cs:
As shown in the figure,
After running, the shooting asteroid and explosion effect after collision will appear.
- Batch generation of asteroids:
Here we need to use a coroutine method:
Create a new game object named gamecontroller, create a script gamecontroller.cs:
Set the required parameters according to the scene.
Run it, and the asteroids can be generated randomly.
- Create a data transfer between different scripts, create a game lifecycle: record scores, game over, and restart.
Add the following code in as_move.cs:
Add the following code in gamecontroller.cs:
Set up as follows in the scene:


Thus, a small space spaceship game is basically completed.

Run the game:
