这篇文章主要为大家详细介绍了Unity实现打砖块游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Unity实现打砖块游戏的具体代码,供大家参考,具体内容如下
效果演示
1.创建墙
1.1我们用预制体来统一管理墙
方便以后对墙进行修改
1.2我们还需要给砖块一个刚体组件(物理属性),不然墙就固定在那里不动。
1.3 把砖块弄出来 再弄成一堵墙
2.发射子弹
我们将子弹也用预制体的方式创造。
这时就到了我们写代码的时候了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shoot : MonoBehaviour
{
public GameObject bullet;//定义游戏物体 (子弹)
float speed = 30;//子弹速度
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))//是否按下鼠标左键
{
GameObject b=GameObject.Instantiate(bullet,transform.position,transform.rotation);//生成子弹的位置
Rigidbody rd = b.GetComponent<Rigidbody>();//得到生成子弹的刚体组件
rd.velocity = transform.forward * speed;//为子弹施加速度
}
}
}
将这段代码给摄像机就可以在运行游戏时按下鼠标左键发射子弹
3.移动发射
将以下脚本代码给摄像机就可以完成移动了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class hv: MonoBehaviour
{
// 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)/60);//移动速度
}
}
4.检验
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
织梦狗教程
本文标题为:Unity实现打砖块游戏


基础教程推荐
猜你喜欢
- C# 解析XML和反序列化的示例 2023-04-14
- Unity shader实现高斯模糊效果 2023-01-16
- C#中 Json 序列化去掉null值的方法 2022-11-18
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C#中的Linq to JSON操作详解 2023-06-08
- 实例详解C#实现http不同方法的请求 2022-12-26
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30