太空大战–声音与特效

主角子弹发射音效

选择主角游戏体,在菜单栏中选择Component->Audio->Audio Source组件。凡是需要发声的游戏体都需要该组件。

打开Player.cs脚本,添加代码。
1、添加声音源组件属性,和声音文件属性

//声音源
protected AudioSource m_audio;//子弹发射声音文件
public AudioClip m_shootClip;

2、在Start函数里获取游戏体的声音源组件

void Start()
{m_transform = this.transform;m_audio = GetComponent<AudioSource>();
}

3、在Update函数里添加播放声音的代码

if(Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
{//创建子弹实例Instantiate(m_rocket, m_transform.position, m_transform.rotation);//播放声音m_audio.PlayOneShot(m_shootClip);}

将声音文件shoot.wav和m_shootClip关联。

关联完成后运行游戏,这时发射子弹就会有声音播放。

主角死亡特效

打开Player.cs,添加以下代码。
1、添加爆照特效的属性

//爆炸特效
public Transform m_explosionFX;

2、在OnTriggerEnter函数中添加创建爆炸特效的代码

private void OnTriggerEnter(Collider other)
{if(other.tag != "PlayerRocket"){m_life -= 1;if(m_life <= 0){Destroy(this.gameObject);Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);}}
}

将爆炸特效属性与explosion.prefab文件关联。

这时主角死亡时就会产生爆炸的特效。

高级敌人发射子弹音效

选择Enemy2b游戏体和Enemy游戏体,按照之前的步骤添加Audio Source组件。
打开Player.cs,添加代码如下。
1、添加声音源组件属性

//声音源
protected AudioSource m_audio;

2、在Start函数里获取游戏体的声音源组件

void Start()
{m_transform = this.transform;m_audio = GetComponent<AudioSource>();
}

打开SuperEnemy.cs,添加代码如下。
1、添加声音文件属性

//子弹发射声音文件
public AudioClip m_shootClip;

2、在UpdateMove函数中添加播放声音文件代码

protected override void UpdateMove()
{//朝着主角方向前进transform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime));m_fireTimer -= Time.deltaTime;if(m_fireTimer <= 0){m_fireTimer = 2;if(m_player != null){//使用向量减法来获取朝向主角的向量方向Vector3 v = m_player.transform.position - transform.position;Instantiate(m_rocket, transform.position, Quaternion.LookRotation(v));//播放声音文件m_audio.PlayOneShot(m_shootClip);}else{GameObject obj = GameObject.FindGameObjectWithTag("Player");if(obj != null){m_player = obj.transform;}}}
}

将Shoot.wav与Enemy2b下的m_shootClip绑定。

敌人死亡特效

打开Enemy.cs,添加代码如下
1、添加爆炸特效的属性

//爆炸特效
public Transform m_explosionFX;

2、修改OnTriggerEnter函数,添加创建爆炸特效代码

void OnTriggerEnter(Collider other)
{//如果撞到的是主角子弹if (other.tag == "PlayerRocket") {//获取该子弹的Rocket属性Rocket rocket = other.GetComponent<Rocket>();if(rocket != null){m_life -= rocket.m_power;               if (m_life <= 0){Destroy(this.gameObject);Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);}}}//如果撞到的是主角飞船else if(other.tag == "Player"){Destroy(this.gameObject);}
}

将explosion.prefab文件和Enemy下的m_explosionFX关联。

关联后敌人阵亡时就会产生爆炸的特效了。

完整的Player.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Player : MonoBehaviour
{//控制飞行速度public float m_speed = 1;//获取transform游戏组件Transform m_transform;//用来获取子弹的prefabpublic Transform m_rocket;//用来控制子弹的发射频率float m_rocketTime = 0;//主角生命public int m_life = 3;//声音源protected AudioSource m_audio;//子弹发射声音文件public AudioClip m_shootClip;//爆炸特效public Transform m_explosionFX;// Start is called before the first frame updatevoid Start(){m_transform = this.transform;//获取声音源组件m_audio = GetComponent<AudioSource>();}// Update is called once per framevoid Update(){//x轴方向移动的距离float moveh = 0;//z轴方向移动的距离float movev = 0;if (Input.GetKey(KeyCode.UpArrow)){movev += Time.deltaTime * m_speed;}if (Input.GetKey(KeyCode.DownArrow)){movev -= Time.deltaTime * m_speed;}if (Input.GetKey(KeyCode.LeftArrow)){moveh -= Time.deltaTime * m_speed;}if(Input.GetKey(KeyCode.RightArrow)){moveh += Time.deltaTime * m_speed;}m_transform.Translate(new Vector3(moveh, 0, movev));m_rocketTime -= Time.deltaTime;if(m_rocketTime <= 0){m_rocketTime = 0.1f; //控制发射频率为隔0.1秒可以发射一次//按住空格键或者鼠标左键可以发射子弹if(Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)){//创建子弹实例Instantiate(m_rocket, m_transform.position, m_transform.rotation);//播放声音m_audio.PlayOneShot(m_shootClip);}}}private void OnTriggerEnter(Collider other){if(other.tag != "PlayerRocket"){m_life -= 1;if(m_life <= 0){Destroy(this.gameObject);Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);}}}
}

完整的Enemy.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Enemy : MonoBehaviour
{//敌人的速度public float m_speed = 1.0f;//敌人的生命public int m_life = 10;//获取敌人的transform组件Transform m_transform;//获取敌人的Renderer组件internal Renderer m_renderer;//判断是否激活internal bool m_isActiv = false;//声音源protected AudioSource m_audio;//爆炸特效public Transform m_explosionFX;// Start is called before the first frame updatevoid Start(){m_transform = this.transform;m_renderer = GetComponent<Renderer>();m_audio = GetComponent<AudioSource>();}private void OnBecameVisible(){m_isActiv = true;}// Update is called once per framevoid Update(){UpdateMove();if(m_isActiv && !m_renderer.isVisible){Destroy(this.gameObject);}}protected virtual void UpdateMove(){//让敌人左右移动float rx = Mathf.Sin(Time.time) * Time.deltaTime;//让敌人朝主角方向前进m_transform.Translate(new Vector3(rx, 0, -m_speed * Time.deltaTime));}void OnTriggerEnter(Collider other){//如果撞到的是主角子弹if (other.tag == "PlayerRocket") {//获取该子弹的Rocket属性Rocket rocket = other.GetComponent<Rocket>();if(rocket != null){m_life -= rocket.m_power;if (m_life <= 0){Destroy(this.gameObject);Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);}}}//如果撞到的是主角飞船else if(other.tag == "Player"){Destroy(this.gameObject);}}
}

完整的SuperEnemy.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SuperEnemy : Enemy
{// Start is called before the first frame update// 重写父类中的MoveUpdate方法//开火频率protected float m_fireTimer = 2;//获取主角属性protected Transform m_player;//获取子弹属性public Transform m_rocket;//子弹发射声音文件public AudioClip m_shootClip;protected override void UpdateMove(){//朝着主角方向前进transform.Translate(new Vector3(0, 0, -m_speed * Time.deltaTime));m_fireTimer -= Time.deltaTime;if(m_fireTimer <= 0){m_fireTimer = 2;if(m_player != null){//使用向量减法来获取朝向主角的向量方向Vector3 v = m_player.transform.position - transform.position;Instantiate(m_rocket, transform.position, Quaternion.LookRotation(v));//播放声音文件m_audio.PlayOneShot(m_shootClip);}else{GameObject obj = GameObject.FindGameObjectWithTag("Player");if(obj != null){m_player = obj.transform;}}}}
}

太空大战--声音与特效相关推荐

  1. 视频教程-Unity经典案例再现《太空大战》-Unity3D

    Unity经典案例再现<太空大战> 专注于VR/游戏研发八年,精通各种常用语言,熟练使用Unity和UE4引擎开发. 张建飞 ¥12.00 立即订阅 扫码下载「CSDN程序员学院APP」, ...

  2. 太空大战--敌人创建

    太空大战–敌人创建 前言 该博客为记录学习太空大战unity项目的过程. 游戏介绍 在游戏中,主角操作太空飞船和敌人的太空飞船战斗.消灭敌人的飞船可以取得一定的分数,游戏没有尽头,如果主角的飞船被击落 ...

  3. 太空大战--游戏ui和战斗管理

    太空大战–游戏UI和战斗管理 创建显示得分的UI界面 在Hierarchy视图中选择Create->UI->Canvas创建一个UI的根节点. 选中创建的Canvas,选择Create-& ...

  4. 中国电子学会青少年编程能力等级测试图形化四级编程题:太空大战

    「青少年编程竞赛交流群」已成立(适合6至18周岁的青少年),公众号后台回复[Scratch]或[Python],即可进入.如果加入了之前的社群不需要重复加入. 我们将有关编程题目的教学视频已经发布到抖 ...

  5. 2021-11-04太空大战项目制作

    太空大战项目制作 一.背景制作 1.创建一个Quad,更名为BG,并为其添加材质 2.设置旋转和放大参数 3.创建一个Quad,更名为BG2,并为其添加材质,作为BG的子对象,设置Y轴位置为-1 4. ...

  6. [源码和文档分享]基于VC++的WIN32 API界面编程实现的飞机太空大战小游戏

    一.程序功能介绍 炫酷精美的飞机太空大战,为体现"设计"的原创性,团队不使用游戏引擎,也没有抄袭任何源代码.全局使用团队自行编写的游戏类对象编写程序.建立以WIN32分辨率960* ...

  7. 基于VC++的WIN32 API界面编程实现的飞机太空大战小游戏

    一.程序功能介绍 炫酷精美的飞机太空大战,为体现"设计"的原创性,团队不使用游戏引擎,也没有抄袭任何源代码.全局使用团队自行编写的游戏类对象编写程序.建立以WIN32分辨率960* ...

  8. 初学者Unity项目--太空大战

    太空大战算是比较经典的游戏了.这两天在跟着视频自学了一下.能做到的效果就是飞机发出子弹打爆陨石,如果被陨石碰到就死掉.简单的赤果果.界面如下: 现在做个总结:(模型声音之类的是导入的资源包.) 很明显 ...

  9. scratch实现太空大战

    今天我们来实现一个scratch拓展中的画笔功能. 视频讲解在这里: scratch实现太空大战效果 来看一下我们今天的实现效果吧. 今天我们要实现在scratch中利用熊猫躲过障碍,最终胜利的效果. ...

  10. 【C语言游戏】太空大战 | SpaceWar(基于EasyX图形库,FPS优化,碰撞判断,drawAlpha绘制透明贴图,音乐播放,源码素材免费分享)

    1. 数据结构介绍 //飞船的数据结构(包括己方战机和敌机) struct aircraft { int x;//横坐标 int y;//纵坐标 int HP;//飞船血量 int spead;//飞 ...

最新文章

  1. Quartz.Net 调度框架配置介绍
  2. 计算机组成原理的判断题,2《计算机组成原理A》判断题
  3. python dpkt解析ssl流
  4. htmltest~计算器界面的实现
  5. C++——this指针
  6. 对话jQuery之父John Resig:JavaScript的开发之路
  7. 提高文档翻译效率神器:VS Code 插件之 Translator Helper
  8. 一个action类中写多个方法需要继承MappingDispatchAction
  9. Oracle exp/imp导出导入命令及数据库备份
  10. ros indigo 学习笔记
  11. Apache Flink 在蔚来汽车的应用
  12. PSpice应用 软件安装和B-1
  13. Java常见笔试题(99.9%必问)
  14. ProtoBuf生成EmmyLua注解API提示文件(支持复杂的嵌套结构)
  15. 方差公式初三_九年级同步数学公式:方差公式(1)
  16. 充值校园卡显示服务器异常,调查| 为何这次校园卡系统故障时间这么长?
  17. [HOJ1864]Fibonacci
  18. python输出星号等腰三角形_Python 打印矩形、直角三角形、等腰三角形、菱形
  19. java发送邮件连接超时,Java邮件超时和连接超时处理
  20. wow Warlock shushia

热门文章

  1. java实现倒计时_Java实现倒计时代码
  2. Unity3D游戏高性能战争迷雾系统实现
  3. 那个职员建议他们去计算机博物馆英语,第三单元重点句子
  4. 阿里P7被裁员,找工作小半年了,流程走着走着就没了
  5. Adobe InDesign繁体字转简体字
  6. mysql虚拟列表_动态网页制作-官方版合集下载-多特
  7. Windows系统下安装VMware Workstation并创建Xubuntu虚拟环境
  8. 如何关闭“数据执行保护”?
  9. 【iOS开发】——weak底层原理
  10. 线性代数——矩阵的秩