Unity3d實(shí)現(xiàn)Flappy Bird游戲
本文實(shí)例為大家分享了Unity3d實(shí)現(xiàn)Flappy Bird的具體代碼,供大家參考,具體內(nèi)容如下
一、小鳥(niǎo)
在游戲中,小鳥(niǎo)并不做水平位移,而是通過(guò)障礙物的移動(dòng)讓小鳥(niǎo)有水平運(yùn)動(dòng)的感覺(jué),小鳥(niǎo)只需要對(duì)鼠標(biāo)的點(diǎn)擊調(diào)整豎直加速度就可以了,同時(shí)加上水平旋轉(zhuǎn)模仿原版的FlappyBird的運(yùn)動(dòng)。同時(shí),還要對(duì)豎直位置進(jìn)行判斷,否則游戲不能正常結(jié)束。
這里貼上小鳥(niǎo)上附加的腳本代碼
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; //確保只執(zhí)行一次
private int bestScore = 0;
// Use this for initialization
void Start () {
body = transform.GetComponent<Rigidbody>();
}
void OnCollisionEnter(Collision collisionInfo)
{
if (state)
{
//碰撞游戲結(jié)束
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);
}
}
//判斷鼠標(biāo)左鍵點(diǎn)擊或者空格
if (Input.GetKeyDown(KeyCode.Space)||Input.GetMouseButtonDown(0))
{
AudioManager.instance.PlayFly();
body.velocity = Vector3.zero;
//加速度
body.AddForce(jumpForce);
//控制旋轉(zhuǎn)量
this.transform.rotation = Quaternion.Euler(45, 270, 0);
}
else
{
//旋轉(zhuǎn)
if (transform.rotation.eulerAngles.x >= 280||transform.rotation.eulerAngles.x<=50)
{
transform.Rotate(-150 * Time.deltaTime, 0, 0);
}
}
}
public void EndGame()
{
//保存最佳成績(jī)
PlayerPrefs.SetInt("PlayerScore", Score.instance.score);
bestScore = PlayerPrefs.GetInt("PlayerBestScore");
if (Score.instance.score > bestScore)
bestScore = Score.instance.score;
PlayerPrefs.SetInt("PlayerBestScore", bestScore);
//跳轉(zhuǎn)到結(jié)束場(chǎng)景
Application.LoadLevel("End");
}
}
二、障礙物
障礙物只要定時(shí)產(chǎn)生,隨機(jī)設(shè)定偏移量,然后添加向左運(yùn)動(dòng)的速度就行了,同時(shí)要設(shè)定自動(dòng)銷(xiāo)毀的時(shí)間,回收障礙物,否則內(nèi)存占用會(huì)越來(lái)越大。
這里用了三個(gè)腳本,分別是上下障礙物和障礙物生成腳本。附加到一個(gè)空物體上就行了。
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;
}
}
此外還有分?jǐn)?shù)顯示,最佳分?jǐn)?shù)顯示,音效等等,都是細(xì)節(jié)。


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#連接SQLite數(shù)據(jù)庫(kù)并實(shí)現(xiàn)基本操作
本文介紹了SQLite,一個(gè)輕量級(jí)的跨平臺(tái)數(shù)據(jù)庫(kù)管理系統(tǒng),以及如何在C#中使用System.Data.SQLite庫(kù)進(jìn)行操作,包括創(chuàng)建、修改和查詢(xún)數(shù)據(jù)庫(kù),以及使用SQLiteHelper類(lèi)簡(jiǎn)化SQL使用,此外,還提到了DB文件查看工具SQLiteSpy的應(yīng)用,需要的朋友可以參考下2024-12-12
C# Linq延遲查詢(xún)的執(zhí)行實(shí)例代碼
這篇文章主要介紹了C# Linq延遲查詢(xún)執(zhí)行的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
C#利用delegate實(shí)現(xiàn)Javascript的each方法
這篇文章主要為大家介紹了介紹了C#利用delegate實(shí)現(xiàn)Javascript的each方法,感興趣的朋友可以參考一下2016-01-01
C#實(shí)現(xiàn)綁定DataGridView與TextBox之間關(guān)聯(lián)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)綁定DataGridView與TextBox之間關(guān)聯(lián)的方法,涉及C#綁定控件關(guān)聯(lián)性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C# Dictionary和SortedDictionary的簡(jiǎn)介
今天小編就為大家分享一篇關(guān)于C# Dictionary和SortedDictionary的簡(jiǎn)介,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10

