Unity实现打砖块游戏
作者:大聪明深夜a题 时间:2023-12-06 04:45:04
本文实例为大家分享了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.检验
来源:https://blog.csdn.net/qq_62045667/article/details/122520507
标签:unity,打砖块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java过滤器模式原理及用法实例
2023-03-07 12:15:07
![](https://img.aspxhome.com/file/2023/3/93353_0s.png)
详解Spring全局异常处理的三种方式
2023-11-11 15:31:47
java后台利用Apache poi 生成excel文档提供前台下载示例
2023-05-15 08:51:45
![](https://img.aspxhome.com/file/2023/9/75749_0s.jpg)
Struts2实现文件上传时显示进度条功能
2021-10-13 05:22:22
深入同步访问共享的可变数据分析
2023-05-08 00:16:49
Android应用中绘制圆形头像的方法解析
2022-02-06 00:46:52
![](https://img.aspxhome.com/file/2023/8/97848_0s.png)
idea中的Maven导包失败问题解决方案汇总
2023-07-12 12:34:22
![](https://img.aspxhome.com/file/2023/7/70567_0s.png)
实例详解SpringBoot默认的JSON解析方案
2023-07-21 07:34:20
详解Java 自动装箱与拆箱的实现原理
2022-08-16 11:35:51
Java队列篇之实现数组模拟队列及可复用环形队列详解
2021-09-12 17:49:42
![](https://img.aspxhome.com/file/2023/1/118821_0s.png)
Java实现布隆过滤器的方法步骤
2023-02-15 20:31:47
![](https://img.aspxhome.com/file/2023/4/61264_0s.jpg)
Android SDK Manager解决更新时的问题 :Failed to fetch URL...
2023-03-17 23:23:36
C#创建自签名认证文件的方法
2021-12-02 03:17:03
c#使用listbox的详细方法和常见问题解决
2023-08-27 11:28:56
![](https://img.aspxhome.com/file/2023/9/68069_0s.jpg)
Spring Security实现HTTP认证
2021-10-31 14:21:47
![](https://img.aspxhome.com/file/2023/0/73010_0s.jpg)
Mybatis关于动态排序 #{} ${}问题
2023-09-01 17:34:57
Springboot webscoket自定义定时器
2023-02-12 05:34:02
![](https://img.aspxhome.com/file/2023/7/109937_0s.png)
viewpager+photoview实现图片查看器
2022-10-30 11:46:05
![](https://img.aspxhome.com/file/2023/4/139434_0s.gif)
Java中左移和右移问题图文详解
2023-02-14 15:44:05
![](https://img.aspxhome.com/file/2023/3/64113_0s.png)
Java验证时间格式是否正确方法类项目实战
2021-05-30 02:15:15
![](https://img.aspxhome.com/file/2023/8/86778_0s.jpg)