Today, I will implement a 3D brick-breaking game. It is very simple and a serious introductory tutorial, but what I learned is the key content of Unity game development. Let’s take a look at it together.
New 3D game project
Open the Unity Hub
software, we create a new 3D project, double-click to open it after creating it.
Initialize the project directory
Create the following folder in the Assets
Scenes
: Store game scenes, the default is to create a new project, if not, create a new oneMaterals
: storage materials, such as a car with different colors and stylesPrefab
: Store prefabs, there will be a lot of bricks and bullets in the bricks, we can set prefabs for themScripts
: Store the game script file, the script contains the running logic of the game.
Create the ground
Create a new Plane
, and create a new material with the same name in Assets/Materals to add style to our ground. If the ground is too small, you can find the Transform group in the property panel on the right and modify the zoom value. X is 20 and Z is 20.
Create a brick wall
Create a new Cube
, then set it to a Prefab , and add a physics engine "rigid body" component (let it have physical effects, free fall, etc.).
Duplicate multiple Cube
to turn them into a wall, and make a group as Cubes
.
Create bullet
Similarly, prefab
, and set Component "Physics" Rigid Body for it, so that it also has physical effects.
Our requirement is such that when the left mouse button is pressed, a bullet will be created, and its animation (movement and brick collision) will be executed, and it will be shot in the front direction of the camera.
In order to achieve the above requirements, create a new script file Shoot.cs
, the code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
public GameObject bullet;
public float speed = 10;
void Update()
{
//按下鼠标左键
if (Input.GetMouseButtonDown(0))
{
GameObject b = GameObject.Instantiate(bullet, transform.position, transform.rotation);
Rigidbody rgd = b.GetComponent<Rigidbody>();
rgd.velocity = transform.forward * speed;
}
}
}
Code analysis:
- Two public attributes are created, bullet is the bullet and speed is the speed at which the bullet is fired
- update is the life cycle of the frame update, which is executed about 60 times per second after the game is running
- In the update method, we monitor the left mouse button, then get the body of the bullet, get its rigid body, set its rigid body speed, and its direction is straight ahead
transform.forward
.
Now the bullet can be launched, but now the bullet can only be launched in one direction, let’s solve this problem.
Control camera
Create a new MoveCamera.cs
script to control the camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
public float speed = 10;
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.Translate(new Vector3(h, v, 0) * Time.deltaTime * speed);
}
}
Code analysis:
- Similarly, we have a
speed
attribute to control the camera movement speed - In the update method, we monitor the up, down, left, and right buttons, and then set the displacement for the camera, change its lens direction, so as to change the direction of the ball.
At this point, our Unity introductory tutorial is over, but if we want to learn the game well, we have to draw inferences and expand its functions.
Expand the brick-breaking function
- AI automatically generates walls of different positions and sizes. (Now it is written directly)
- Add a variety of styles of bullets, which can be different colors or different shapes.
- Added bullet firing sound effects and wall collision sound effects.
- Add game scores.
- Add multiple scenes, walls of different difficulty.
- Add a store, you can buy all kinds of bullets.
So much has been written here, and there will be time to implement the extended functions later, so stay tuned!
Summarize
This brick-and-mortar game uses some common knowledge points in Unity game development, such as the use of life cycle, monitoring user input, obtaining world coordinates of objects and changing coordinates, etc. The following APIs should be focused on ( Very commonly used):
Vector3
Input
GameObject
Extended reading
If you don’t know what Unity is, please read first:
over!
I’m Geek Ape Xiaobing, the public account [Geek Ape], I record the learning and growth of independent developers (game development/product development/reverse/operation design).
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。