初始Unity 3D——小Demo打砖块
来源:互联网 发布:一元提现微信红包软件 编辑:程序博客网 时间:2024/06/10 10:57
需求:鼠标点击墙壁,从观察者角度发射子弹,击中墙壁砖块。子弹在五秒钟后消失
从小Demo中学习解决问题的逻辑思路:
1:使用prafab循环创建一堵墙
2:监听鼠标与墙壁的“碰撞“事件
3:发射子弹
1、循环创建墙壁
声明public变量,将砖块brick拖进去
void CreateWall(){ for(int i=0;i< width;i++){ for(int j=0;j< height;j++){ GameObject.Instantiate(brick,newVector3(i.j.0),Quaternion.identity) } }}
2、发射射线,检测两个碰撞
//从主摄像机发射射线到鼠标左键点击的位置ray=Camera.main.ScreenPointToRay(Input.position);if(Physics.Raycast(ray,out hit)){ ...}
3、发射子弹
//创建子弹:GameObject b=GameObject.Instantiate(bullet,camera位置);//获取子弹发射路线,也就是一个向量v=hit.position-camera位置;//沿着路径发射子弹b.GetComponent<Rigidbody>().AddForce(v*100);
还是感觉没得写,因为步骤简单,想要记住代码的话需要多去实践。下面贴出完整代码。
using UnityEngine;using System.Collections;public class RayTest2 : MonoBehaviour { public GameObject brick; public GameObject bullet; private int width=10; private int height = 6; private Ray ray; private RaycastHit hit; private Transform m_Transform; void Start () { m_Transform = gameObject.GetComponent<Transform>(); CreateWall(); } void Update () { Fire(); //发射子弹 } /// <summary> /// 发射子弹 /// </summary> void Fire() { if (Input.GetMouseButtonDown(0)) { ray = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out hit)) { //GameObject.Destroy(hit.collider.gameObject); //让砖块消失 //获取子弹发射的路线 Vector3向量 Vector3 v = hit.point - m_Transform.position; GameObject tempBullet = GameObject.Instantiate(bullet, m_Transform.position, Quaternion.identity) as GameObject; tempBullet.GetComponent<Rigidbody>().AddForce(v*100); } } } /// <summary> /// for循环创建墙壁 /// </summary> void CreateWall() { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { GameObject.Instantiate(brick, new Vector3(i-5, j, 0), Quaternion.identity); } } }}
下面是子弹的脚本
using UnityEngine;using System.Collections;public class Bullet : MonoBehaviour { void Start () { GameObject.Destroy(gameObject, 5); } void Update () { }}
0 0
- 初始Unity 3D——小Demo打砖块
- 打砖块Demo
- (结对作业)基于Unity 3D游戏引擎开发的“打砖块”游戏
- Unity 2D Arkanoid Tutorial(打砖块教程)
- 初始Unity 3D——刚体常用方法
- 初始Unity 3D——碰撞事件的监听
- 初识Unity 3D——小小小Demo(上)
- Unity 官方案例 打砖块
- Box2D例子——打砖块
- 重温经典——打砖块
- Unity 3D 泰课网打砖块实例 可以用来发射炮弹
- Unity 官方案例 打砖块(二)
- Unity学习之打砖块小游戏
- Unity 弹球打砖块游戏(简易)
- 初始Unity 3D——飞盘射击案例(2)
- 初始Unity 3d(Getting.Started.with.Unity 读书笔记一)
- 打砖块
- 打砖块
- 国外空间VPS云测试比较
- 播放视频,播放音质,计算坐标
- 2016年11月30日 数据库mysql 8个小时自动断开
- 使用OpenCv操作&分析像素的基础核心操作
- Android打电话直接拨号、跳转到拨号面板、跳转到联系人
- 初始Unity 3D——小Demo打砖块
- 安卓与html混合开发之原生与js相互调用
- @ComponentScan注释类型的参数含义,包含excludeFilters,includeFilters等其他参数
- python2.7 安装pypcap出错 pcap.h not found
- 浅谈hadoop(四)——hadoop简介
- python爬去易车网某地市经销商信息
- Android Dagger2 MVP架构 一看就明白
- Leetcode441. Arranging Coins
- iOS原生二维码扫描(二)