This is the 30th day of my participation in the August Text Challenge.More challenges in August

I’ve written a couple of small game blogs in Unity before, and I feel good about it, so I’m going to keep writing. Today write a brick primary version of the small game.

In fact, this game is modeled after the traditional brick breaking game. But the theme of the game is under the sea. A turtle catches the ball as a baffle and bounces it to hit other seafloor creatures floating upstream. The game interface is shown below.

In order to enrich the content of the game, let the game elements more. This small game uses the level system. Finish one level and move on to the next. And the difficulty of each level is different, the monster is different, the number of monsters is different, and the blood of monsters is different.

It’s a small game, but it’s also visceral. Let’s take a look at its design process.

The first thing I will introduce is the objects in the scene.

The objects in the scene have the above parts of the process, which are the camera, the day off light, the background, the ball, the turtle, and the Gameover icon. The camera is like the eye, it is necessary, without the camera, you can’t see what is happening in the scene. There is also the day off light is parallel light, like the sun irradiation, if the scene is not, it will be a dim light. I’m making a 2D game, so all I need to do is import the background image, set it to Sprite, select the middle of the scene, and make it bigger. Also in the Game window I used my own proportions to make it look like a window display for a mobile game.

void Start() { Random.InitState((int)System.DateTime.Now.Ticks); X = Random. Range (9.0 3.0 f, f); y = Mathf.Sqrt(vec * vec - x * x); rigid = gameObject.GetComponent<Rigidbody2D>(); rigid.velocity = new Vector2(x, y); } / / Update is called once per frame void the Update () {if (this. Transform. LocalPosition. > > 0 f && x 2.8 x | | Enclosing the transform. LocalPosition. < 2.8 x && x < 0 f) {x = - x; rigid.velocity = new Vector2(x, y); } the if (this. Transform. LocalPosition. Y && y < 0 < 3.31 f) {Time. The timeScale = 0; Gameover. Transform. LocalPosition = new Vector2 (2.0 0.1 f, f); } the if (this. Transform. LocalPosition. Y > 6.5 f && y > 0) {y = - y; rigid.velocity = new Vector2(x, y); }}Copy the code

This is the key code for the ball. What this basically says is that the ball hits the wall and then bounces, which requires a change in the direction of velocity.

Void Update() {// Get the mouse position in the camera (world) and convert it to screen coordinates; screenPosition = Camera.main.WorldToScreenPoint(transform.position); MousePositionOnScreen = Input. MousePosition; // make the scene Z= mouse coordinates Z mousepositionOnscreen. Z= screenPosition. Z; / / the coordinates of the Camera into the world coordinate mousePositionInWorld = Camera. Main. ScreenToWorldPoint (mousePositionOnScreen); Transform. position = mousePositionInWorld; Transform. position = new Vector3(mousepositionInWorld.x, transform.position.y, transform.position.z); }Copy the code

This paragraph above is the key code of the turtle baffle. Basically, let the turtle move along with the table.

void OnTriggerEnter2D(Collider2D collider) { if (collider.gameObject.tag == "spark" && Time.frameCount - timelast > 15) { timelast =Time.frameCount; collider.gameObject.GetComponent<spark>().lift -= 1; y = -y; rigid.velocity = new Vector2(x, y); } else if (y <0) { y = -y; rigid.velocity = new Vector2(x, y); }}Copy the code

Let’s look at the collision detection function in this ball. The tag of the collision object is Spark, where Spark refers to the monster. If it hits it, it will lose blood. This health is a global attribute on the monster. Monsters are destroyed when their health reaches zero. When there are no monsters in the scene, it’s the end of a level.

This little game isn’t too hard. If you are a novice to Learn Unity at that time, you can understand it. After that, I have a certain understanding of physical system and collision detection. If you are interested in learning Unity, you can also follow the public account: Poetic Code and find me to learn together.