[Unity3D game development] "Arkanoid" game development tutorial

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/q764424567/article/details/99434394

I. Introduction

The original "Arkanoid" was released in 1986, having been so long, but still very addictive, because of the nature of its arcade-style, it is very easy to develop in Unity.

As usual, everything pieces will explain as simply as possible, so that everyone can understand.

Here is a preview of the game:
Here Insert Picture Description

2, version

Version of the project: Unity 2018.3.14f1
updated version can also be a good use of the error some of the API can not be used that may occur if you are using an older version, then, in order to obtain better results, please use the Unity 2018.3.14f1 version

Third, Tutorials

1, the camera settings

First, we find the main camera scene Main Camera, and then adjust the background color is dark, and modify the size as shown in the afternoon:
Here Insert Picture Description

2, set the background

Note: Right-click the image and select Save As, find the assets folder of the project and save it to a new Sprite folder.
Here Insert Picture Description

Let us in the project area:
Here Insert Picture Description
this picture covered the entire screen

3, hierarchical set

We're working on a 2D game, in some cases, there are several images are painted on top of each other, for example, when the ball and the background patterns are painted.

Let us tell union, our background pattern should be in the background to ensure that the ball always drawn on top (and therefore visible).

We can use Sorting Layer, change the background picture of the sort layer

Let us choose to add sorting layer .. Sorting Layer from the list, then add Background layer, and moved to a first position, as shown below:
Here Insert Picture Description

Let us find the background image of Sprite Renderer Components, and assign our new sort Background layer:
Here Insert Picture Description

4, add borders

Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description
选择图片另存为,把资源导入到项目中 Sprite文件夹中

Here Insert Picture Description
然后把这些边界摆放到场景中:
Here Insert Picture Description
添加边界的碰撞:
选中边界:
Here Insert Picture Description
添加碰撞器:
Add Component -> Physics 2D -> Box Collider 2D
Here Insert Picture Description

5、添加球拍

球拍图像
Here Insert Picture Description
注意:选择图片,另存图片,选择另存为,将其保存到项目中的Sprite文件夹中

将球拍图片放置到场景中,调整位置,X0,Y-95,Z0.
Here Insert Picture Description
球拍的碰撞器:
选中球拍,添加碰撞器
Add Component -> Physics 2D -> Box Collider 2D
Here Insert Picture Description
选中球拍,添加刚体
Add Component -> Physics 2D -> Rigidbody 2D
Here Insert Picture Description
Gravity Scale:0(防止球拍出界)
Collision Detection:Contonuous 连续(防止因物理二维引擎bug而使球穿过球拍的小故障)
在Z轴上冻结旋转:这使球拍冻结了角度。

6、球拍移动

我们添加一个Racket.cs脚本:
Here Insert Picture Description
双击脚本来打开它:

using UnityEngine;
using System.Collections;

public class Racket : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

我们的Start函数在开始游戏时由统一自动调用。我们的Update函数一次又一次地自动调用,大致与默认设置为60的游戏帧速率相关联。

还有另一种类型的Update函数,它被称为FixedUpdate,它也被一次又一次的调用,但是在一个固定的时间间隔内。单位物理是在完全相同的时间间隔内计算的,所以使用它总是一个好主意FixedUpdate在做物理相关的计算时,因为我们的球拍是一个物理物体(因为它有一个刚体)。

我们可以移除Start和Update函数并创建一个FixedUpdate函数:

using UnityEngine;
using System.Collections;

public class Racket : MonoBehaviour {

    void FixedUpdate () {

    }
}

我们可以使用刚体组件Rigidbody的velocity属性控制球拍移动
移动的原理:Speed * 运动方向
Here Insert Picture Description
让我们先添加一个速度变量到我们的脚本中:

using UnityEngine;
using System.Collections;

public class Racket : MonoBehaviour {
    // Movement Speed
    public float speed = 150;

    void FixedUpdate () {

    }
}

玩家可以使用键盘上的A/D控制左右移动,检查玩家输入的函数可以使用Unity自带的GetAxisRaw函数来获取水平输入,它会返回来一个-1 到1之间的值,-1为左边,1为右边

void FixedUpdate () {
    // Get Horizontal Input
    float h = Input.GetAxisRaw("Horizontal");
}

现在我们可以设置运动方向乘以速度:

void FixedUpdate () {
    // Get Horizontal Input
    float h = Input.GetAxisRaw("Horizontal");

    // Set Velocity (movement direction * speed)
    GetComponent<Rigidbody2D>().velocity = Vector2.right * h * speed;
}

将Racket脚本,附加到球拍物体上面。
接下来我们按下Play,我们现在可以移动球拍了:
Here Insert Picture Description

7、这个球

球的图片
Here Insert Picture Description
注意:选择图片,另存为,加入到项目中Sprite文件中
摆放到场景中:
Here Insert Picture Description

添加碰撞器:Add Component -> Physics 2D -> Box Collider 2D
Here Insert Picture Description
球也应该从墙壁上弹起。如果它直接飞向墙壁,那么它就会反弹到相反的方向。如果它撞到墙上45°角度,然后它应该在一个-45°夹角(等等)…这背后的数学非常简单,可以用脚本来完成,但是我们将通过分配一个物理材料球对撞机。物理材料包含有关对撞机物理方面的信息,如摩擦和弹性。

让我们右键单击项目区,选择Create -> Physics Material 2D给它起个名字球料:Here Insert Picture Description
之后我们可以看看Inspector并调整材料性能以使其反弹:
Here Insert Picture Description
现在我们只需要把材料从项目区进入材料球的对撞机插槽:
Here Insert Picture Description

添加刚体:Add Component -> Physics 2D -> Rigidbody 2D
Here Insert Picture Description
Mass:0.0001(这将有助于防止球推开球拍)
Linear Drag:0(这将防止球使用重力)
Interpolate:Interpolate插值(使物理尽可能精确)
Collision Detection:Continuous连续(有助于防止因统一物理2D引擎bug而产生的碰撞错误)

8、添加球的脚本

Add Component -> New Script->Ball.cs

接着我们双击打开它:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

我们不需要更新函数,所以让我们删除它:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }
}

让我们用刚体的速度属性使其移动。向上由某人速度:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {
    // Movement Speed
    public float speed = 100.0f;

    // Use this for initialization
    void Start () {
        GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
    }
}

将Ball.cs赋给 球物体
我们可以按下播放键,看球从边框弹出:
Here Insert Picture Description

9、球拍碰撞角

当球击中球拍时,我们希望球员能够对球的外角有一定的控制:

Here Insert Picture Description

让我们打开球脚本,这样我们就可以实现出角功能了。当球与其他东西碰撞时,我们会用OnCollisionEnter2D函数,Unity自动调用的函数:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {
    // Movement Speed
    public float speed = 100.0f;

    // Use this for initialization
    void Start () {
        GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
    }

    void OnCollisionEnter2D(Collision2D col) {
        // This function is called whenever the ball
        // collides with something
    }
}

球的速度取决于它击中球拍的位置。

y值永远是1,因为我们希望它飞向顶部,-1是飞向底部。x值是它击中球拍的位置,也就是它的速度。

1  -0.5  0  0.5   1  <- x值取决于它被击中的位置
===================  <- 这是那个球拍

我们真正需要做的就是找出球在哪里,相对于球拍。我们可以通过简单的分割球来做到这一点x球拍坐标宽度…这是我们的功能:

float hitFactor(Vector2 ballPos, Vector2 racketPos,
                float racketWidth) {
    // ascii art:
    //
    // 1  -0.5  0  0.5   1  <- x value
    // ===================  <- racket
    //
    return (ballPos.x - racketPos.x) / racketWidth;
}

Note: we subtract the value of the ball racket PO.x posse value, in order to obtain the relative position.
This is our OnCollisionEnter2D function:

void OnCollisionEnter2D(Collision2D col) {
    // Hit the Racket?
    if (col.gameObject.name == "racket") {
        // Calculate hit Factor
        float x=hitFactor(transform.position,
                          col.transform.position,
                          col.collider.bounds.size.x);

        // Calculate direction, set length to 1
        Vector2 dir = new Vector2(x, 1).normalized;

        // Set Velocity with dir * speed
        GetComponent<Rigidbody2D>().velocity = dir * speed;
    }
}

If we press play, so we can now depend on the direction of the ball on the bounce it hits the racket position.

10, adding block

We will use the following image as our block:

Blue: Here Insert Picture DescriptionGreen: Here Insert Picture Descriptionpink: Here Insert Picture Descriptionred: Here Insert Picture Descriptionyellow:Here Insert Picture Description

We put these resources into the project to go

Add impactor: Add Component -> Physics 2D -> Box Collider 2D

Add a new script Block.cs

Then we open it:

using UnityEngine;
using System.Collections;

public class Block : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

We can remove the boot and updates, because we do not need them. We are already familiar with OnCollisionEnter2D function, so let's use it again:

using UnityEngine;
using System.Collections;

public class Block : MonoBehaviour {

    void OnCollisionEnter2D(Collision2D collisionInfo) {
        // Destroy the whole Block
        Destroy(gameObject);
    }
}

Note: Destruction (this) will only undermine the script block assembly. If we want to destroy the whole neighborhood, then we have to use to destroy the (game objects), which will destroy the block to the game itself.

Then we spread this block go into the scene:

Here Insert Picture Description

Then press the play button!

Congratulations, you have completed your own 2D game Arkanoid.

Guess you like

Origin blog.csdn.net/q764424567/article/details/99434394