Unity3d實現(xiàn)Flappy Bird游戲
本文實例為大家分享了Unity3d實現(xiàn)Flappy Bird的具體代碼,供大家參考,具體內(nèi)容如下
一、小鳥
在游戲中,小鳥并不做水平位移,而是通過障礙物的移動讓小鳥有水平運動的感覺,小鳥只需要對鼠標(biāo)的點擊調(diào)整豎直加速度就可以了,同時加上水平旋轉(zhuǎn)模仿原版的FlappyBird的運動。同時,還要對豎直位置進(jìn)行判斷,否則游戲不能正常結(jié)束。
這里貼上小鳥上附加的腳本代碼
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)左鍵點擊或者空格
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()
{
//保存最佳成績
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é)束場景
Application.LoadLevel("End");
}
}
二、障礙物
障礙物只要定時產(chǎn)生,隨機設(shè)定偏移量,然后添加向左運動的速度就行了,同時要設(shè)定自動銷毀的時間,回收障礙物,否則內(nèi)存占用會越來越大。
這里用了三個腳本,分別是上下障礙物和障礙物生成腳本。附加到一個空物體上就行了。
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é)。


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# Hashtable/Dictionary寫入和讀取對比詳解
本文中將從基礎(chǔ)角度講解HashTable、Dictionary的構(gòu)造和通過程序進(jìn)行插入讀取對比2013-11-11
C#入門學(xué)習(xí)之集合、比較和轉(zhuǎn)換
本文詳細(xì)講解了C#中的集合、比較和轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C#實現(xiàn)Access通用訪問類OleDbHelper完整實例
這篇文章主要介紹了C#實現(xiàn)Access通用訪問類OleDbHelper,結(jié)合完整實例形式分析了C#針對access數(shù)據(jù)庫的連接、查詢、遍歷、分頁顯示等相關(guān)操作技巧,需要的朋友可以參考下2017-02-02

