国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity3d實現(xiàn)Flappy Bird游戲

2019-10-29 19:32:21
字體:
供稿:網(wǎng)友

本文實例為大家分享了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)生,隨機(jī)設(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é)。

Unity3d,Flappy,Bird

Unity3d,Flappy,Bird

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 云安县| 扶沟县| 馆陶县| 全南县| 景宁| 焉耆| 赤城县| 罗甸县| 镇平县| 平阳县| 福泉市| 且末县| 南京市| 赤峰市| 富阳市| 东山县| 潜山县| 射洪县| 湘乡市| 定西市| 类乌齐县| 沙雅县| 汉源县| 宁国市| 北京市| 色达县| 连南| 和平区| 禹城市| 华宁县| 和龙市| 延安市| 山东| 桃园县| 恭城| 改则县| 钟山县| 日照市| 九龙城区| 安宁市| 伊金霍洛旗|