打砖块游戏

素材下载

打砖块游戏素材:下载

STARTO!

首先建立一个 2D 专案

更改 MainCamera 的颜色为黑色 (0, 0, 0, 0),并且变更大小为 125

建立 Sprites 资料夹,将开头下载的素材拉进来

将全部的素材全选

将所有图片的每单位像素数改为 1

选择套用

将 hexagon_pattern.png 拉入阶层视窗

更改 hexagon_pattern 物件的属性 Sprite Renderer > 其他设定 > 排序图层,选择 Add Sorting Layer…

按下 +

输入 Background

并把 Background 往上拉一个层级

并将 hexagon_pattern 物件的排序图层改为 Background

将 border_top.png 拉进阶层视窗

将 border_top 物件的 位置 Y 改为 115

将 border_left.png 拉进阶层视窗

将 border_left 的位置 X 改为 -107

将 border_right.png 拉进阶层视窗

将 border_right 的位置 X 改为 107

游戏画面完成

在阶层视窗同时选取三个边框

按下增加元件

2D 物理 > 2D 盒状碰撞器

将 racket.png 拉进阶层视窗

将 racket 物件的位置 Y 设定为 -100

将 racket 物件新增元件 > 2D 物理 > 2D 盒状碰撞器

将 racket 物件新增刚体属性。新增元件 > 2D 物理 > Rigidbody 2D 并且冻结 Z 轴

将身体类型改为 Kinematic

新增一个 Scripts 资料夹,在里面新增一个脚本文件名为 Racket.cs

点两下编辑 Racket.cs 脚本,并且输入以下程式码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Racket : MonoBehaviour
{
    // Start is called before the first frame update
    public float speed = 150;
    void Start()
    {
        
    }

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

    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        GetComponent<Rigidbody2D>().velocity = Vector2.right * h * speed;
    }
}

将 Racket.cs 脚本拉到 racket 物件底下

将 ball.png 拉进阶层视窗

更改 ball 物件的位置 Y 为 -80

将 ball 物件新增元件 > 2D 物理 > 圆形碰撞器

在资源管理器的 Assets 资料夹中按下滑鼠右键 > 建立 > 物理材质 2D,名为 BallMaterial

修改 Friction(摩擦系数) 为 0,Bounciness(反弹力) 为 1

在 ball 物件的圆形碰撞器中新增 BallMaterial 材质

将 ball 物件加入刚体属性。增加元件 > 2D 物理 > Rigidbody 2D

修改品质为 0.0001;重力大小设定为 0;碰撞侦测改为持续;插值改为插值

将开头下载的音效拉进素材视窗

将 ball 物件增加元件 > 音讯 > 音讯源,并选择刚刚拉入的音讯源

将唤醒时播放勾勾拿掉

新增一个 Ball 脚本,输入以下程式码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{
    // Start is called before the first frame update
    public float speed = 100.0f;
    void Start()
    {
        GetComponent<Rigidbody2D>().velocity = Vector2.up * speed;
    }
    float hitFactor(Vector2 ballPos, Vector2 racketPos, float racketWidth)
    {
        return (ballPos.x - racketPos.x) / racketWidth;
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        GetComponent<AudioSource>().Play();
        if (col.gameObject.name.Equals("racket")){
            float x = hitFactor(transform.position,
                col.transform.position,
                col.collider.bounds.size.x);
            Vector2 dir = new Vector2(x, 1).normalized;
            GetComponent<Rigidbody2D>().velocity = dir * speed;
        }
    }
    // Update is called once per frame
    void Update()
    {

    }
}

将脚本拉进 ball 物件底下

将 block_xx.png 随机的颜色放到阶层视窗

将 block_xx 物件的位置修改为 X 为 -96;Y 为 90

将 block_xx 物件新增元件 > 2D 物理 > 2D 盒状碰撞器

新增 Block.cs 脚本,并输入以下程式码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Block : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    void OnCollisionEnter2D(Collision2D collisionInfo)
    {
        Destroy(gameObject);
    }
}

将 Block.cs 脚本拉进 block_xx 物件中

完成

SHXJ
Latest posts by SHXJ (see all)

在〈打砖块游戏〉中有 1 则留言

发布留言