Unity3d实现Flappy Bird游戏
程序员文章站
2023-12-01 14:18:22
本文实例为大家分享了unity3d实现flappy bird的具体代码,供大家参考,具体内容如下
一、小鸟
在游戏中,小鸟并不做水平位移,而是通过障碍物的移动让小鸟有水...
本文实例为大家分享了unity3d实现flappy bird的具体代码,供大家参考,具体内容如下
一、小鸟
在游戏中,小鸟并不做水平位移,而是通过障碍物的移动让小鸟有水平运动的感觉,小鸟只需要对鼠标的点击调整竖直加速度就可以了,同时加上水平旋转模仿原版的flappybird的运动。同时,还要对竖直位置进行判断,否则游戏不能正常结束。
这里贴上小鸟上附加的脚本代码
player.cs
using unityengine; using system.collections; public class player : monobehaviour { private rigidbody body; public vector3 jumpforce = new vector3(0, 300, 0); private bool state = true; //确保只执行一次 private int bestscore = 0; // use this for initialization void start () { body = transform.getcomponent<rigidbody>(); } void oncollisionenter(collision collisioninfo) { if (state) { //碰撞游戏结束 state = false; score.instance.state = false; audiomanager.instance.playhit(); audiomanager.instance.playdie(); invoke("endgame", 0.4f); } } // update is called once per frame void update () { //下限 if (transform.position.y < -20) { if (state) { state = false; score.instance.state = false; audiomanager.instance.playdie(); invoke("endgame", 0.4f); } } //上限 if (transform.position.y > 20) { if (state) { state = false; score.instance.state = false; audiomanager.instance.playdie(); invoke("endgame", 0.4f); } } //判断鼠标左键点击或者空格 if (input.getkeydown(keycode.space)||input.getmousebuttondown(0)) { audiomanager.instance.playfly(); body.velocity = vector3.zero; //加速度 body.addforce(jumpforce); //控制旋转量 this.transform.rotation = quaternion.euler(45, 270, 0); } else { //旋转 if (transform.rotation.eulerangles.x >= 280||transform.rotation.eulerangles.x<=50) { transform.rotate(-150 * time.deltatime, 0, 0); } } } public void endgame() { //保存最佳成绩 playerprefs.setint("playerscore", score.instance.score); bestscore = playerprefs.getint("playerbestscore"); if (score.instance.score > bestscore) bestscore = score.instance.score; playerprefs.setint("playerbestscore", bestscore); //跳转到结束场景 application.loadlevel("end"); } }
二、障碍物
障碍物只要定时产生,随机设定偏移量,然后添加向左运动的速度就行了,同时要设定自动销毁的时间,回收障碍物,否则内存占用会越来越大。
这里用了三个脚本,分别是上下障碍物和障碍物生成脚本。附加到一个空物体上就行了。
generateobstacle.cs
using unityengine; using system.collections; public class genrateobstacle : monobehaviour { public gameobject obstacle; public gameobject obstacle1; public float starttime = 1f; public float gaptime=1.5f; public float gapdistance = 13; private vector3 gapvector; private vector3 midvector; // use this for initialization void start () { invokerepeating("initiateobstacle", starttime, gaptime); gapvector = new vector3(0, gapdistance / 2, 0); } void initiateobstacle() { midvector = new vector3(8,random.range(-3.2f, 3.2f),0); instantiate(obstacle, midvector+gapvector,new quaternion(0,0,180,0)); instantiate(obstacle1, midvector-gapvector, quaternion.identity); } }
obstacle.cs
using unityengine; using system.collections; public class obstacle : monobehaviour { public float speed = -8f; private rigidbody body; private transform player; private bool ispassed = false; // use this for initialization void start () { destroy(this.gameobject, 4); body = this.getcomponent<rigidbody>(); body.velocity = new vector3(speed, 0, 0); player = gameobject.findgameobjectwithtag("bird").transform; } // update is called once per frame void update () { if (player.transform.position.x > transform.position.x && ispassed == false) { ispassed = true; score.instance.getscore(); } } }
obstacle1.cs
using unityengine; using system.collections; public class obstacle1 : monobehaviour { public float speed = -8f; private rigidbody body; private transform player; private bool ispassed = false; // use this for initialization void start() { destroy(this.gameobject, 4); body = this.getcomponent<rigidbody>(); body.velocity = new vector3(speed, 0, 0); player = gameobject.findgameobjectwithtag("bird").transform; } }
此外还有分数显示,最佳分数显示,音效等等,都是细节。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 手动编译C#代码的方法
下一篇: js点击弹出div层实现可拖曳的弹窗效果