[Unity]2D打飞机游戏
来源:互联网 发布:通天绳解密 知乎 编辑:程序博客网 时间:2024/06/10 03:28
今天带来一个在自学Unity3D过程中写的第一个2D打飞机小游戏。代码写得不好,功能也很少,主要是为了记录学习心得,也希望各位能够多给些指导。后续应该会有优化和完善游戏功能。
闲话不多说,先来分析下游戏:
几乎没有什么功能,没有UI。主要有玩家方向键控制飞机移动,空格键射击,敌人被击中后销毁。
1.创建工作层
为了使游戏对象在场景中的层次不会错乱,可以创建不同的工作层来管理游戏对象,从而使2D游戏场景的层次更加分明,游戏对象之间的操作也更加方便。
依次选择菜单栏中的Edit -> Project Settings -> Tags and Layers (或者直接单机编辑器右上方的Layers -> Edit Layers),在Inspector视图中,单击Sorting Layers,添加如图两个Layer
顺便简单说下layer和sorting layer的区别:
layer主要通过光线投射来选择性地忽略碰撞器,或者添加碰撞功能。而sorting layer就是一个渲染层级的顺序的控制。决定Unity渲染关系的层级顺序是 :Camera ,soting layer sorting order。
2.创建背景
Assets文件夹中,导入Sprite文件夹,里面是我们需要用到的图片资源
直接将背景图片拖拽到Scene中,该图片自动成为一个精灵,改名为sky。将其sorting layer设置为background, order设置为0。为了实现背景循环滚动的效果,我们添加再添加两张背景图,上下叠放。
因为背景图片每单位100像素,MaxSize2048,所以每张图片高度应该是20.48
三张背景图的y坐标分别是20.48,0,-20.48,这样就可以将背景图无缝叠在一起。创建一个空对象,将三张背景图作为它的子物体,命名为Background。
接下来编写脚本使背景循环滚动来模拟飞机在场景中不断前进的效果。为Background对象添加该脚本组件。
using System.Collections;public class BGControl : MonoBehaviour { //背景滚动速度 public float speed = 1.5f;void Update () { transform.Translate(Vector3.down * Time.deltaTime * speed); if (transform.position.y <= -20.48f) { transform.position += new Vector3(0, 20.48f*2, 0); }}}
3.创建玩家飞机
将玩家飞机拖拽到场景视图中。将其sorting layer设置为froeground,order设置为1。
为其编写控制脚本
using UnityEngine;using System.Collections;public class PlayerControl : MonoBehaviour { //玩家移动速度 public float speed=5f; //子弹发射点 public GameObject shotPoint; //子弹prefab public GameObject bullet; //玩家发射子弹CD private float shotCD = 0; public float CD = 0.2f; //声音资源 private AudioSource audio;// Use this for initializationvoid Start () { audio = GetComponent<AudioSource>();}// Update is called once per framevoid Update () { //----------------玩家移动---------------- //向上移动 if (Input.GetKey(KeyCode.UpArrow) && transform.position.y<=0f) { transform.Translate(Vector3.up * Time.deltaTime * speed); } //向下移动 if (Input.GetKey(KeyCode.DownArrow) && transform.position.y>=-8f) { transform.Translate(Vector3.down * Time.deltaTime * speed); } //向左移动 if (Input.GetKey(KeyCode.LeftArrow) && transform.position.x>=-7f) { transform.Translate(Vector3.left * Time.deltaTime * speed); } //向右移动 if (Input.GetKey(KeyCode.RightArrow) && transform.position.x<=7f) { transform.Translate(Vector3.right * Time.deltaTime * speed); } //发射炮弹 if (Input.GetKey(KeyCode.Space)) { shotCD -= Time.deltaTime; shot(); } } void shot() { //CD结束发送炮弹 if (shotCD <= 0) { Instantiate(bullet, shotPoint.transform.position, Quaternion.Euler(new Vector3(0, 0, 0))); audio.Play(); //重置CD shotCD = CD; } }}
using UnityEngine;using System.Collections;public class Shell : MonoBehaviour { // private AudioSource audio; //子弹速度 public float speed=10f; //爆炸特效 public GameObject effect;// Use this for initializationvoid Start () { //audio = gameObject.GetComponent<AudioSource>();}// Update is called once per framevoid Update () { //炮弹移动 transform.Translate(Vector3.up * speed * Time.deltaTime); //销毁炮弹 if (transform.position.y >= 11f) { Destroy(gameObject); }} void OnCollisionEnter2D(Collision2D obj) {<span style="white-space:pre"></span>
<span style="white-space:pre"></span>//实例化粒子特效 GameObject neweffect = Instantiate(effect, transform.position, transform.rotation) as GameObject; Destroy(obj.gameObject); Destroy(gameObject); Destroy(neweffect, 1.0f); }}
在这里,当子弹击中敌人时,播放了粒子特效。需要注意的是,在特效的Particle System组件中,Renderer的sorting layer需要设置为foreground,order设置为1.否则特效会被掩盖。
4.敌人控制
创建5个prefab,层级关系和玩家的一样。
简单的脚本来随机生成一种敌机,挂载在Main Camera上
using UnityEngine;using System.Collections;public class GameControl : MonoBehaviour { public GameObject[] enemy;// Use this for initializationvoid Start () { Invoke("CreateEnemy", 1.0f);}// Update is called once per framevoid Update () { } //生成敌人 void CreateEnemy() { int enemyType = Random.Range(0, enemy.Length); Vector3 enemyPos=new Vector3(Random.Range(-7f,7f),11f,0); Instantiate(enemy[enemyType], enemyPos, Quaternion.Euler(new Vector3(0, 0, 0))); Invoke("CreateEnemy", 1.0f); }}
脚本控制让敌人往下移动。
using UnityEngine;using System.Collections;public class EnemyControl : MonoBehaviour { //敌人速度 public float speed = 6f;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () { //敌人移动 transform.Translate(Vector3.down * speed * Time.deltaTime); if (transform.position.y <= -11) { Destroy(gameObject); }}}
一个功能简单的lowbee 2D打飞机游戏就完成了!
- [Unity]2D打飞机游戏
- unity打飞机游戏简单制作
- Unity打飞机小游戏
- 2D 太空打飞机游戏 Bitfighter源码下载(C/C++ Lua)
- 一个3D ar打飞机的游戏iOS源码
- C#打飞机游戏
- 人工智能--打飞机游戏
- unity 3d 射击游戏 (一)场景和飞机
- Unity打飞机基本脚本
- 【Unity3D 游戏】 打飞机(仿微信打飞机) 源码
- 发布一个打飞机游戏
- 摄像头互动游戏-打飞机
- 开源游戏 “打飞机”
- Python开发打飞机游戏
- Pygame开发打飞机游戏
- 打飞机游戏相关策划
- Unity 2D游戏制作
- 汇编写的打飞机游戏
- java 利用printf格式化输出
- 判断pc端还是移动端
- Handler处理线程间通讯的原理
- 网约车在美国疯狂崛起 出租车越来越不景气
- UI组件:ImageView及其子类
- [Unity]2D打飞机游戏
- BZOJ4377[POI2015] Kurs szybkiego czytania
- 美国政府交出互联网域名管理权
- 充值信仰的时刻到了:索尼2018年推多款手机游戏
- android快速开发常见框架
- 雅虎黑客事件严重 Verizon表示可能放弃收购
- oracle 内存分配和调优 总结
- Json介绍
- 点击checkbox保留账号和密码