汕头市公司网站建设多少钱,谷歌系平台推广,做视频网站要多大带宽,页面设计时最好由Siki学院打砖块游戏启发完成一个非常非常简单#xff0c;纯新手也能十分钟做出来的小游戏——打砖块。 一.搭建场景 首先我们先在一个空白的3D项目中创建一个Plane平面#xff0c;将其放置于世界中央位置#xff0c;长宽设置为2#xff0c;并为其添加一个材质Material纯新手也能十分钟做出来的小游戏——打砖块。 一.搭建场景 首先我们先在一个空白的3D项目中创建一个Plane平面将其放置于世界中央位置长宽设置为2并为其添加一个材质Material将其改为黑色改名为ground 然后创建一个标准的Cube将其position的y设置为0.5放于ground正上方为其添加一个材质Material改成紫色改名为brick 创建一个预制体文件夹 将brick拖入Prefab文件夹中制成预制体 通过ctrlD复制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 updatevoid Start(){}// Update is called once per framevoid 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 updatevoid Start(){}// Update is called once per framevoid 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 updatevoid Start(){//GameObject.Instantiate(Bullet_Prefab, transform.position, transform.rotation);}// Update is called once per framevoid Update(){if(Input.GetMouseButtonDown(0)){//创建一个子弹GameObject BulletGameObject.Instantiate(Bullet_Prefab, transform.position, transform.rotation);//获取子弹的刚体组件Rigidbody rd Bullet.GetComponentRigidbody();//直接赋予刚体一个速度rd.velocity Vector3.forward * 30;}}
}再为Brick预制体添加上rigidbody游戏就制作完成了。
四.效果