Unity 4.3 制作一个2D横版射击游戏(2)
在上一篇《Unity 4.3 制作一个2D横版射击游戏》时,已经完成一个基本的射击游戏了。在这一篇将继续完善。
1.视差卷轴效果
- 主角随着摄像机向前推进
- 背景元素除了摄像机的移动外,又以不同的速度移动
Layer | Loop | Position |
Background with the sky | Yes | (0, 0, 10) |
Background (1st row of flying platforms) | No | (0, 0, 9) |
Middleground (2nd row of flying platforms) | No | (0, 0, 5) |
Foreground with players and enemies | No | (0, 0, 0) |
如下图所示:
接下来,先实现下不带背景循环的滚动。创建一个脚本,命名为"ScrollingScript",代码如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using UnityEngine;
/// <summary> /// <summary> /// <summary> void Update() movement *= Time.deltaTime; // Move the camera |
Layer | Speed | Direction | Linked to Camera |
0 - Background | (1, 1) | (-1, 0, 0) | No |
1 - Background elements | (1.5, 1.5) | (-1, 0, 0) | No |
2 - Middleground | (2.5, 2.5) | (-1, 0, 0) | No |
3 - Foreground | (1, 1) | (1, 0, 0) | Yes |
- 添加两个天空背景到0 - Background
- 添加一些小的平台到1 - Background elements
- 添加一些平台到2 - Middleground
- 添加更多的敌人到3 - Foreground,放置在摄像机的右边
1
2 3 4 5 6 7 8 9 10 |
using UnityEngine;
public static class RendererExtensions |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
using System.Collections.Generic;
using System.Linq; using UnityEngine; /// <summary> /// <summary> /// <summary> /// <summary> /// <summary> // 3 - Get all the children for (int i = 0; i < transform.childCount; i++) // Add only the visible children // Sort by position. void Update() movement *= Time.deltaTime; // Move the camera // 4 - Loop if (firstChild != null) // Set the position of the recyled one to be AFTER // Set the recycled child to the last position |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
using UnityEngine;
/// <summary> void Awake() // Retrieve scripts to disable when not spawn // 1 - Disable everything // Disable everything void Update() // 4 - Out of the camera ? Destroy the game object. // 3 - Activate itself. // Enable everything |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
void Update()
{ // ... // 6 - Make sure we are not outside the camera bounds var leftBorder = Camera.main.ViewportToWorldPoint( var rightBorder = Camera.main.ViewportToWorldPoint( var topBorder = Camera.main.ViewportToWorldPoint( var bottomBorder = Camera.main.ViewportToWorldPoint( transform.position = new Vector3( // End of the update method |
2.粒子效果
Category | Parameter name | Value |
General | Duration | 1 |
General | Max Particles | 15 |
General | Start Lifetime | 1 |
General | Start Color | Gray |
General | Start Speed | 3 |
General | Start Size | 2 |
Emission | Bursts | 0 : 15 |
Shape | Shape | Sphere |
Color Over Lifetime | Color | See below (N°1) |
Size Over Lifetime | Size | See below (N°2) |
Category | Parameter name | Value |
General | Looping | false |
General | Duration | 1 |
General | Max Particles | 10 |
General | Start Lifetime | 1 |
General | Start Speed | 0.5 |
General | Start Size | 2 |
Emission | Bursts | 0 : 10 |
Shape | Shape | Box |
Color Over Lifetime | Color | See below (N°1) |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
using UnityEngine;
/// <summary> public ParticleSystem smokeEffect; void Awake() Instance = this; /// <summary> // Tu tu tu, tu tu tudu // Fire in the sky /// <summary> // Make sure it will be destroyed return newParticleSystem; |
这里创建了一个单例,可以让任何地方都可以产生烟和火焰的粒子。将这个脚本附加到"Scripts"对象,设置其属性"Smoke Effect"和"Fire Effect"为对应的预制体。现在是时候调用这个脚本了,打开"HealthScript"脚本文件,在OnTriggerEnter方法里面,更新成如下代码:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public Damage(int damageCount)
{ // ... if (hp <= 0) // Dead! // ... |
3.音乐和音效
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using UnityEngine;
using System.Collections; /// <summary> /// <summary> public AudioClip explosionSound; void Awake() public void MakeExplosionSound() public void MakePlayerShotSound() public void MakeEnemyShotSound() /// <summary> |
1
|
SoundEffectsHelper.Instance.MakeExplosionSound();
|
1
|
SoundEffectsHelper.Instance.MakePlayerShotSound();
|
1
|
SoundEffectsHelper.Instance.MakeEnemyShotSound();
|
4.菜单
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using UnityEngine;
/// <summary> // Draw a button to start the game |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
using UnityEngine;
/// <summary> if ( if ( |
1
2 3 4 5 6 7 |
void OnDestroy()
{ // Game Over. // Add the script to the parent because the current game // object is likely going to be destroyed immediately. transform.parent.gameObject.AddComponent<GameOverScript>(); } |
现在运行,当死亡时,就会出现按钮,如下图所示:
如文章存在错误之处,欢迎指出,以便改正。转载请注明出处。
Unity 4.3 制作一个2D横版射击游戏(2)相关推荐
- Unity 4 3 制作一个2D横版射击游戏 2
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 在上一篇 ...
- Unity 4 3 制作一个2D横版射击游戏
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 这是一个 ...
- Unity 4.3 制作一个2D横版射击游戏
这是一个横版射击游戏. 教程来自: http://pixelnest.io/tutorials/2d-game-unity/ 中文版教程: http://www.litpo.com/categor ...
- Unity3D开发一个2D横版射击游戏
教程基于http://pixelnest.io/tutorials/2d-game-unity/ , 这个例子感觉还是比较经典的, 网上转载的也比较多. 刚好最近也在学习U3D, 做的过程中自己又修改 ...
- Unity学习笔记3 简易2D横版RPG游戏制作(三)
这一篇本来应该是在上一篇后面直接补进去的.不过因为排版的问题.所以我就另开一篇来整理了,好了,废话不多说,马上整理: 十八.关卡的锁定与解锁 前面我们已经解决了在游戏开始时可以选择关卡的问题,接下来我 ...
- unity3d游戏2d横版射击游戏完整项目源码分享
unity3d游戏2d横版射击游戏完整项目源码分享 免费下载地址: 链接:https://pan.baidu.com/s/1YwhEy7DeKIHFU8pBLdJFPg 提取码:3wnx 复制这段内容 ...
- Unity学习笔记1 简易2D横版RPG游戏制作(一)
这个教程是参考一个YouTube上面的教程做的,原作者的教程做得比较简单,我先参考着做一遍,毕竟我也只是个初学者,还没办法完全自制哈哈.不过我之前也看过一个2D平台游戏的系列教程了,以后会整合起来,做 ...
- Unity学习笔记2 简易2D横版RPG游戏制作(二)
十二.敌人受攻击时的闪烁和Player的生命值的修正 上一篇中,我们利用Controller2D中的IEnumerator TakenDamage接口,使得我们的Player受到攻击时会进行闪烁,我们 ...
- 【CSON原创】基于HTML5的横版射击游戏发布
功能说明: 基于HTML5的横版射击游戏,参考自flash游戏<双面特工>.左右方向键控制移动,下方向键蹲下,上方向键跳跃,空格键射击.体验前请先关闭输入法. 该游戏基于自己开发的HTML ...
最新文章
- 谷歌最新论文:从图像中进行3-D目标检测
- 网络延迟和数据包丢失对网络性能的影响—Vecloud微云
- c# 学习笔记 (2) 窗体之间互相调用的方法
- TEMPO研究第一年影像学数据: 骨侵蚀修复几乎只出现在无关节肿胀或肿胀改善组...
- 微信红包随机数字_微信红包的随机算法
- Tensorflow入门----占位符、常量和Session
- 年薪 37 万+,挪威科技大学(NTNU)招募博士,利用 ML 和 AI 进行钢桥结构健康监测和损伤检测...
- 写好代码,拒绝一传到底的参数类
- 整洁数据 —tidytext(一)
- 这 4 款电脑记事本软件,得试试
- linux 中meltdown指令,用代码切身实践体会meltdown漏洞——初学者的体验感受
- 登陆apple pay显示无法登录服务器,apple pay无法添加卡怎么办?未能连接到apple pay的解决方法...
- 高级系统架构师培训笔记
- Eclipse/Myeclipse自定义JSP模板
- nginx证书绑定及二级域名反向代理
- 室内定位技术之UWB篇
- 实战:无线wifi密码破解与路由器入侵
- 扫地机器人半湿拖布_居然能自己洗拖布?云鲸智能扫拖一体机帮你真正解放双手...
- linux 查找某个程序,Linux查找特定程序whereis实例详解
- 怎样在Word文档中插入GIF动画