新手十分钟也能完成的Unity小游戏打砖块

   由Siki学院打砖块游戏启发完成一个非常非常简单,纯新手也能十分钟做出来的小游戏——打砖块。

一.搭建场景

        首先我们先在一个空白的3D项目中创建一个Plane平面,将其放置于世界中央位置,长宽设置为2,并为其添加一个材质Material,将其改为黑色;改名为ground

        然后创建一个标准的Cube,将其position的y设置为0.5,放于ground正上方;为其添加一个材质Material,改成紫色,改名为brick;

        创建一个预制体文件夹

                        ​​​​​​​        ​​​​​​​        

        将brick拖入Prefab文件夹中制成预制体

        通过ctrl+D复制brick,再按住ctrl步移复制体,最终将其拼成一个6x10的砖墙,砖块可以放入一个空物体下统一管理

二.控制相机的移动

        为相机添加一个移动脚本Movement

编写Movement脚本

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

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

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");//获取键盘的左右移动
        transform.Translate(new Vector3(h, 0, 0));//通过向量改变transform
    }
}

        再Update中我们编写获取键盘输入,并通过translate方法对相机的位置进行改变,实现了相机的左右移动。但是当我们进入游戏实验的时候 ,发现移动速度太快了。这是因为Update方法一秒执行60帧,而GetAxis的变化是从-1~1的变化,乘上60会导致相机移动速度过快。

        所以我们需要一秒移动一米该怎么办呢,很明显我们需要一个帧数的倒数,但是帧数会根据用户电脑性能不同而变化,还好Unity内置了一个这样的功能——Time.deltatime

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

public class Movement : MonoBehaviour
{
    public int speed = 5;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        transform.Translate(new Vector3(h, v ,0) * Time.deltaTime * speed);
        
    }
}

        加上Time.deltaTime后再*速度即可自由控制移动速度。

三.制作小球

        首先我们先制作一个预制体小球,先在场景中添加Spere,再给小球添加一个Materal材质给他改成红色,并命名为Bullet,然后将其拖入预制体Prefab文件夹,并为其添加刚体rigidbody

        然后创建一个脚本,命名为Shoot,添加到MainCamera上

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

public class Shoot : MonoBehaviour
{
    public GameObject Bullet_Prefab;
    // Start is called before the first frame update
    void Start()
    {
        //GameObject.Instantiate(Bullet_Prefab, transform.position, transform.rotation);
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            //创建一个子弹
            GameObject Bullet=GameObject.Instantiate(Bullet_Prefab, transform.position, transform.rotation);
            //获取子弹的刚体组件
            Rigidbody rd = Bullet.GetComponent<Rigidbody>();
            //直接赋予刚体一个速度
            rd.velocity = Vector3.forward * 30;
        }
    }
}

        再为Brick预制体添加上rigidbody,游戏就制作完成了。

四.效果

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值