使用unity编写简单的弹幕游戏【ten seconds】
文章目录
- 效果预览
- 物体移动
- 弹幕发射
- 子弹预制件
- 倒计时
- 场景切换
- 手动换关
- 自动进入下一关
- 退出游戏
- 音乐播放
- 全局音乐
- 音乐名的存放
- 音乐控制/播放器
- 音乐管理器
- 创建音乐管理器实例
- 按照路线走的发射弹幕机
- 会追踪的弹幕发射机
- 导出游戏
- 导出报错解决
学校老师给了个主题是Ten Seconds,所以就写了一个弹幕游戏。
主题就是生存十秒,第一次写了一个完整的小游戏,之前的游戏都没有封面UI什么的,基本只实现了逻辑。
效果预览
选关界面:
(总共搞了14个关卡,每章内容是一个主题,到下一章主题会改变,主题是在每一个关卡生存十秒,生存成功即可进入下一关)
选择不敢就会退出游戏
关卡:
第一章:
第二章:
disanz
第三章:
从这里开始物体会跟随
第四章:物体会追踪玩家的球
搭建场景和贴图很简单就不说了
物体移动
给物体添加角色控制器,然后挂接脚本
PlayerMove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerMove : MonoBehaviour
{public float speed = 10;private CharacterController controller;// Start is called before the first frame updatevoid Start(){controller = GetComponent<CharacterController>();}// Update is called once per framevoid Update(){float x, z;x = Input.GetAxis("Horizontal");z = Input.GetAxis("Vertical");Vector3 move;move = transform.right * x * Time.deltaTime * speed + transform.forward * z * Time.deltaTime * speed;controller.Move(move);}
}
弹幕发射
挂接在中心的cube身上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CubeRotate : MonoBehaviour
{private float rotation = 0;public float rotateSpeed = 2;public float Timer = 0.5f;public GameObject bullet;public float rotaAngle = 0;public float shootTime = 1f;public float shootnum=36;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){Timer += Time.deltaTime;rotation += Timer * rotateSpeed;transform.Rotate(new Vector3(0, rotation, 0));//让中心的物体旋转if (Timer >= shootTime){rotaAngle += 25;//每次发射的球都会发生些许偏移的角度Timer -= shootTime;for (int i = 0; i < shootnum; i++){Quaternion rotateAngle = Quaternion.Euler(0, i * (360/shootnum) + rotaAngle, 0);Instantiate(bullet, transform.position, rotateAngle);}}}
}
(这里需要注意一点,由于界面是俯视界面,但是在球发射的时候可能和人不在一个高度上导致无法射中,所以可以从低空的平视来看看是不是在一个平面上)
子弹预制件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class BulletMove : MonoBehaviour
{public float BulletSpeed = 5;private float Timer = 0;public GameObject Bullet;// Start is called before the first frame updatevoid Start(){GameObject.Destroy(Bullet, 6.0f);//六秒后自动销毁}private void FixedUpdate(){transform.position = transform.position + transform.forward * Time.fixedDeltaTime * BulletSpeed;}// Update is called once per frameprivate void OnTriggerEnter(Collider other){if (other.tag == "Bullet")//防止子弹碰到子弹然后导致子弹消失{return;}else{if (other.gameObject.tag == "Player"){Destroy(other.gameObject);SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//重新加载当前场景}Destroy(this.gameObject);}}
}
倒计时
添加一个text在合适的位置,然后挂接以下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class ChangeText : MonoBehaviour
{public Text daojishitext;public float Timer=0;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){Timer += Time.deltaTime;float NowTime = 10.4f - Timer;daojishitext.text = NowTime.ToString(); }
}
到这里就可以实现一个最简单的弹幕游戏了,接下来为游戏添加一些功能来完善
场景切换
首先需要在File-Build Settings将场景导入,并且场景具有下标
手动换关
在选关界面,设置好了背景之后,需要通过一个画布来实现:
创建一个Panel,(在UI中)
然后添加一个Grid Layout布局
然后其子组件由多个按钮组成:
然后想要使得按下按钮实现关卡切换,需要这样:
首先书写一个脚本里的函数实现关卡切换:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class LL1 : MonoBehaviour
{public void LL(){SceneManager.LoadScene("Level1-1");//直接加载对应名字的关卡,这里也可以直接用数字代表关卡的下标}// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}
}
不要忘了引入头文件:
using UnityEngine.SceneManagement;
然后我们可以将这个脚本拖拽给按钮的“on click”:
但是直接拖拽就会发现无法成功,没有对应函数
我们只能先将脚本挂接在一个空物体上,然后将空物体拖拽给onclick,然后选择按下onclick时会发生的函数,也就是我们上面写好的脚本
同理 其他的关卡选择也是一样的,只是重复的机械操作,这里不再赘述。
自动进入下一关
并且由于主题是ten seconds,意味着只要当时间超过十秒时,就会自动进入下一个场景,因此在每个场景中创建一个空物体然后挂接以下类似的脚本跳转到下一关:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class TenSecondsEnd1 : MonoBehaviour
{private float Timer;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){Timer += Time.deltaTime;if (Timer > 10){SceneManager.LoadScene("Level1-2");}}
}
退出游戏
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuitGame : MonoBehaviour
{public void Quit(){//UnityEditor.EditorApplication.isPlaying = false;Application.Quit();}}
然后按下退出按钮时调用脚本中的函数即可
音乐播放
在场景中的音乐只需要创建空物体然后挂接组件即可:
全局音乐
除此之外,还需要一个自游戏开始时贯穿整个游戏的背景音乐:
参考这篇文章实现:
http://www.voidcn.com/article/p-xaletxkq-bdc.html
需要实现的有两点:
- 在主界面时播放音乐
- 跨场景继续播放音乐
- 再次回到主界面时不会重复播放一样的音乐(防止出现禁忌の二重存在 )
- 循环播放
下面我们使用较为规范的方式来实现,也就是尽可能通过代码直接实现而不是在unity中挂接组件拖拽音乐的方式实现。
需要四个脚本:
- 存放音乐名的类
- 音乐控制/播放器(专门用来播放特定音乐的)
- 音乐管理器(创建一个实例,让管理器这个实例添加音乐控制器的脚本,这样就可以实现一个管理器包含多个音乐播放器)
- 用一个脚本来创建音乐管理器的实例
音乐名的存放
首先将背景音乐放到文件夹下:
虽然可以通过拖入音乐的方式来实现,不过这里想通过脚本的方式自动装载音乐(事实上,在以后公司里这样的代码应该更加规范吧(?),所以需要这样:
我们可以将音乐的名字存放在类中,方便在代码中直接调用:
using UnityEngine;
using System.Collections;public class Global
{public static string musicName="bgMusic";
}
音乐控制/播放器
创建一个空物体,挂接Audio Source组件,然后将音乐拖入至Audio Clip即可播放音乐
这是在unity中的方式,下面使用代码来实现:
using UnityEngine;
using System.Collections;[RequireComponent(typeof(AudioSource))]
public class MusicController : MonoBehaviour
{private AudioClip music;private AudioSource musicPlayer;void Awake(){// 获取 AudioSourcemusicPlayer = this.GetComponent<AudioSource>();// 打开单曲循环musicPlayer.loop = true;// 关闭开始时就播放的属性musicPlayer.playOnAwake = false;}void UpdateMusicSetting(){// 读取音乐music = (AudioClip)Resources.Load(Global.musicName);// 将读取到的音乐赋值给 audioClipmusicPlayer.clip = music;// 播放musicPlayer.Play();}void Start(){UpdateMusicSetting();}
}
上面这段代码如何使用? 我们只需要创建一个实例,然后让这个实例添加这个脚本即可。
音乐管理器
一个音乐管理器可以添加多个音乐播放器,通过这个来实现
AddComponent<MusicController>();
通过这条语句来跨场景保留实例:
DontDestroyOnLoad(instance);
用这个判断防止重复创建:
if (instance == null)
using UnityEngine;
using System.Collections;public class MusicManager : MonoBehaviour
{private static MusicManager instance;private MusicManager() { }public static MusicManager GetInstance(){if (instance == null){// 创建一个游戏对象,命名为 MusicPlayerGameObject musicPlayer = new GameObject("MusicPlayer");// 给 MusicPlayer 附上单例脚本instance = musicPlayer.AddComponent<MusicManager>();// 给 MusicPlayer 游戏对象添加音乐播放器脚本musicPlayer.AddComponent<MusicController>();// 跨场景不删除这个单例DontDestroyOnLoad(instance);}return instance;}
}
创建音乐管理器实例
创建一个空物体,然后挂接这个脚本。这样的话就可以实现了全局音乐了,并且还有音乐管理器,播放器,分工合作而不是全部放在一个脚本里,方便代码复用。
using UnityEngine;
using System.Collections;public class Test : MonoBehaviour
{void Start(){MusicManager.GetInstance();}
}
按照路线走的发射弹幕机
首先需要设定好需要走的路,用目标路径点空物体来承接,然后这些物体挂接在父物体空物体上:
然后对plane烘焙导航
给发射弹幕机cube挂接组件:
巡逻脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Patrol : MonoBehaviour
{// Start is called before the first frame updatepublic float patrolSpeed = 2f;public float patrolWaitTime = 0f;public Transform patrolWayPoints;private NavMeshAgent agent;private float patrolTimer;//这个用来记录当敌人达到路径点的时候 停留时间是多久private int wayPointIndex;//记录当前目标点是哪个路径点void Start(){agent = GetComponent<NavMeshAgent>();}// Update is called once per framevoid Update(){Patrolling();}void Patrolling(){agent.isStopped = false;agent.speed = patrolSpeed;if (agent.remainingDistance <= agent.stoppingDistance)//由于可能这个物体并不是一分不差的到目的点的,我们可以用用一个范围表示冗余//当到达目的点时开始计算时间{patrolTimer += Time.deltaTime;if (patrolTimer >= patrolWaitTime)//如果等待时间已满,则进入下一个点{if (wayPointIndex == patrolWayPoints.childCount - 1){wayPointIndex = 0;}else{wayPointIndex++;}patrolTimer = 0;//由于切换完成 所以我们将这个time归零计算}//这里没有else 是因为如果没=等待时间没满则什么都不会做 只是单单的增加事件}else{patrolTimer = 0;}agent.destination = patrolWayPoints.GetChild(wayPointIndex).position;//Getchild方法可以获得这个东西的子物体//括号里面跟的是下标}}
接下来把上面设定好的路径点传入:
会追踪的弹幕发射机
其实很简单 ,同样需要烘培导航平面和给物体挂接导航体,然后挂接以下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Chase : MonoBehaviour
{private NavMeshAgent agent;public float chaseSpeed = 3f;//追踪的速度private bool PlayerInSight=false;public GameObject Player;// Start is called before the first frame updatevoid Start(){agent = GetComponent<NavMeshAgent>();}// Update is called once per framevoid Update(){Chasing();}private void Chasing(){double distance = Mathf.Sqrt(Mathf.Pow((transform.position.x - Player.transform.position.x), 2) + Mathf.Pow((transform.position.y - Player.transform.position.y), 2));agent.speed = chaseSpeed;if (distance > 3){agent.destination = Player.transform.position;}}}
然后将玩家传入即可
导出游戏
导出报错解决
导出游戏时记得当初遇到了一个问题,报错提示:
error CS0234: The type or namespace name ‘EditorApplication’ does not exist
暂时不知道为什么,百度后解决方案:
File→Build Settings→Player Settings→Player→OtherSettings中的
改成和原来不一样的那个,ctrl+s保存即可
使用unity编写简单的弹幕游戏【ten seconds】相关推荐
- Unity编写冰球对战游戏 2D版
Unity编写冰球对战游戏 2D版 Unity是一款非常方便.简洁.低成本的一款游戏引擎.在这里,向大家介绍这样用Unity这个引擎编写冰球对战游戏 因为Unity这个引擎可以编写3D游戏 ,同样的也 ...
- unity ui框架_用unity制作简单的太空游戏(2)-简单炮台
多铆蒸刚,炮塔至大! 亿万星辰,亿万炮塔! 多铆蒸刚,炮塔至上! 亿万炮塔,亿万荣光! (PS:我没有咕咕咕,就是比较惨,一口气出了半个月的差,人瘦了,也黑了,心塞塞--赶紧写个文章压压惊--) 这一 ...
- C++编写简单的俄罗斯方块游戏
代码地址如下: http://www.demodashi.com/demo/14593.html C++编写简单的俄罗斯方块游戏 使用C++编写一个简单的俄罗斯方块游戏. 1 环境要求 使用C++图形 ...
- 用unity制作简单的太空游戏(1):简单飞船控制
最近沉迷<Dreadnought>(中文名:无畏战舰,B站CG链接),回想起作为十余年EVE老油条的太空生涯,又萌生了做个太空游戏的情怀,所以这一次就讲讲怎么做个简单的太空飞船的小游戏好了 ...
- unity贪吃蛇c 语言代码,unity实现简单贪吃蛇游戏
本文实例为大家共享了unity实现贪吃蛇游戏的详细代码,供大家参考,详细内容如下 首先创建一个头部,编写脚本利用WASD控制头部的移动. Vector3 up=new Vector3(0,1,0); ...
- 用Java编写简单的扑克牌游戏
昨天我帮助一位朋友解决了这个问题,今天我即兴想写一篇详细的关于用Java编写扑克牌游戏的文章. 当然我这里只是实现一些简单的功能. 关于扑克牌游戏的特征,想必大家都有所接触过,有扑克牌,玩扑克牌游戏的 ...
- Unity非常简单的翻牌游戏教程,纯UI实现
首先在场景中创建Canvas,加几个Button 为了排版方便,用了GridLayout 每个Button的结构是这样的,你要为Button里面加四个Image,我这样弄是为了省事,少写脚本 这四个I ...
- Unity 3D-learning 简单打飞碟游戏
一.编写一个简单的打飞碟游戏 游戏内容要求: 游戏有 n 个 round,每个 round 都包括10 次 trial: 每个 trial 的飞碟的色彩.大小.发射位置.速度.角度.同时出现的个数都可 ...
- 使用Java编写简单的老虎机游戏
无论游戏多么简单或复杂 ,Java都能胜任! 在这篇文章中,让我们看一下Java编程的初学者如何制作一个简单而功能齐全的老虎机. 老虎机已经存在很长时间了,但是它的娱乐价值似乎并没有减弱. Inter ...
- Unity:简单易懂小游戏
这篇博文并不讲述整个游戏的制作流程,如果您感兴趣,可以访问这里,如果访问遇到什么困难,在B站有一摸一样的视频. 这是完成以后的样子: 运行效果: 作为个人小结,下面写一些做这个游戏的收获: 1.什么是 ...
最新文章
- pathview包绘制富集的kegg图
- 【大数据】SparkSql 连接查询中的谓词下推处理 (一)
- AC自动机 HDU 2222
- linux solusos 软件包管理工具 eopkg 简介
- SqlServer 批量备份
- 强化学习(三)---马尔科夫决策过程
- 为什么我们会看到 SAP Spartacus 服务器端渲染 `rendering in process` 的日志
- 三星Note2 行货 水货 型号版本
- 自动化运维环境搭建过程
- Java之面试基础知识学习笔记
- Windows编译Nginx源码
- 固态硬盘怎么看出厂日期_如何查看SSD可以使用多长时间?固态硬盘寿命测试方法(全文)...
- hustoj安装学习(2019)
- 在网页中插入视频代码大全
- 基于F4/F7/H7飞控硬件和px4飞控固件的廉价自主无人机系统(1)-飞控
- 粘包问题和struck解决
- 如何准备银行秋招春招?
- 微信小程序腾讯服务器地址要购买吗,微信小程序JavaScript SDK
- 提高团队成员的工作积极性/团队凝聚力
- 联想拯救者刃7000P跑分与加装内存条和SSD固态硬盘