banner
YZ

周周的Wiki

种一棵树最好的时间是十年前,其次是现在。
zhihu
github
csdn

The beginning of a Unity career - Making a spaceship mini-game

First Encounter with a Small Demo Made in Unity

  1. 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.

  2. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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:
Insert image description here
Insert image description here

Thus, a small space spaceship game is basically completed.

Insert image description here

Run the game:

Insert image description here

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.