Unity3d实现Flappy Bird游戏
作者:qq402335257 时间:2023-09-17 20:54:29
本文实例为大家分享了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;
}
}
此外还有分数显示,最佳分数显示,音效等等,都是细节。
来源:https://blog.csdn.net/qq402335257/article/details/51591001
标签:Unity,Flappy,Bird
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android利用传感器实现微信摇一摇功能
2023-07-12 05:05:18
Java 8新的时间日期库的20个使用示例
2022-11-06 06:55:00
浅析C# 装箱和拆箱
2022-02-06 17:43:27
![](https://img.aspxhome.com/file/2023/2/88312_0s.png)
举例讲解设计模式中的访问者模式在Java编程中的运用
2022-04-04 08:00:45
![](https://img.aspxhome.com/file/2023/1/113401_0s.png)
SpringBoot任务之详解邮件任务
2021-08-12 12:49:16
![](https://img.aspxhome.com/file/2023/6/113206_0s.png)
Android ListView适配器(Adapter)优化方法详解
2023-08-09 21:44:32
![](https://img.aspxhome.com/file/2023/6/97856_0s.jpg)
android使用Path绘制出多边形
2021-11-11 19:53:38
![](https://img.aspxhome.com/file/2023/4/123344_0s.png)
C#之CLR内存字符串常量池(string)
2023-04-23 00:09:12
![](https://img.aspxhome.com/file/2023/1/106531_0s.png)
@RequestBody的使用案例代码
2021-07-11 16:46:50
VSCode中开发JavaWeb项目的详细过程(Maven+Tomcat+热部署)
2022-10-18 14:42:10
![](https://img.aspxhome.com/file/2023/1/122281_0s.jpg)
java实现省市区转换成树形结构
2022-11-27 03:43:37
![](https://img.aspxhome.com/file/2023/4/84264_0s.jpg)
c#的dllimport使用方法详解
2023-04-20 04:01:49
Java实现添加条形码到PDF表格的方法详解
2023-04-26 12:37:25
![](https://img.aspxhome.com/file/2023/3/70343_0s.png)
GridView实现桌面图标显示案例
2021-12-24 03:23:08
![](https://img.aspxhome.com/file/2023/9/122549_0s.gif)
从零开始Java实现Parser Combinator
2023-06-18 18:52:04
解读JDK8踩坑JCE加密限制版本问题
2021-09-20 12:42:35
![](https://img.aspxhome.com/file/2023/2/96042_0s.png)
JavaWeb中获取表单数据及乱码问题的解决方法
2021-11-21 11:03:53
![](https://img.aspxhome.com/file/2023/3/69673_0s.png)
spring boot整合netty的实现方法
2021-11-17 04:28:03
![](https://img.aspxhome.com/file/2023/8/83108_0s.jpg)
java中抽象类和接口的相同和不同点介绍
2021-05-30 04:53:31
Java中HashMap里面key为null存放到哪
2023-11-10 02:46:47