软件编程
位置:首页>> 软件编程>> C#编程>> Unity3D实现打砖块游戏

Unity3D实现打砖块游戏

作者:LGX_TvT  发布时间:2021-11-13 11:29:44 

标签:Unity3D,打砖块

本文实例为大家分享了Unity3D实现打砖块的具体代码,供大家参考,具体内容如下

基于unity2017

1、 使用Plane创建初始地图

(层级菜单[Hierarcy]-> 3D Object -> Plane)

2、将Plane命名为Gound

3、 更改Scale

x = 2,y = 2,z = 2,将Plane变为原来的两倍

Unity3D实现打砖块游戏

4、在Assest下新建一个文件夹并命名为Material

用于存放材质

5、创建材质

(右键 -> Create -> Mateial),并命名为Gound

6、设置材质的贴图

1).材质的贴图
2).金属度
3).光滑度

Unity3D实现打砖块游戏

7、设置墙壁

1).在Assets中创建一个Prefab目录。
2).在层级目录中创建一个Cube,并将其设为Prefab,设置为Prefab主要是便于整体修改。
3).创建一个空组件,将Cube作为空组件的子组件,并将空组件命名为Bircks
4).为Prefab的Cube组件添加Rigibody属性(物理属性) (点击add Component搜索 Rigibody)
5).使用Ctrl + D 复制方块 与 Ctrl + 左键 按单元格拖动方块

Unity3D实现打砖块游戏

8、创建 *

在prefab下创建Sphere,同时添加Rigibody属性

9、添加 * 飞出脚本

1).在Assets下创建Script文件夹用于存放脚本
2).右键创建C#脚本,命名为Shoot
3).将Shoot脚本赋予给相机(Main Camera)只需要将脚本拖到Main Camera组件上就可以添加脚本了

Unity3D实现打砖块游戏

4).编辑脚本

public GameObject buttle;//用于获得 * 对象
public float speed = 20F;
? ? // Use this for initialization
? ? void Start () {

? ? }

? ? // Update is called once per frame
? ? void Update () {
? ? ? ? //当鼠标按下左键时触发
? ? ? ? if (Input.GetMouseButtonDown(0))
? ? ? ? {
? ? ? ? ? ? //创建 * 对象
? ? ? ? ? ?GameObject gb = GameObject.Instantiate(buttle,transform.position,transform.rotation);
? ? ? ? ? ?//创建刚体对象
? ? ? ? ? ?Rigidbody rg = gb.GetComponent<Rigidbody>();
? ? ? ? ? ?//设置 * 初始速度
? ? ? ? ? ?rg.velocity = transform.forward * speed;
? ? ? ? }
? ? }

5).赋予脚本Public属性值

只要脚本声明为public的字段,就可以从外部直接得到,这时将准备好的Prefab的Shpere * 赋值给Buttle

Unity3D实现打砖块游戏

10、创建镜头移动脚本

方法同上,创建一个Movement的C#脚本,赋予Main Camera

public class Movement : MonoBehaviour {
? ? public float speed = 5F;
? ? // Use this for initialization
? ? 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);

? ? }
}

11、结束

Unity3D实现打砖块游戏

来源:https://blog.csdn.net/l1336037686/article/details/79689292

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com