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

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

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

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

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

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

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

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