麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

Unity3d實現Flappy Bird游戲

2019-10-29 19:32:21
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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; }  }

此外還有分數顯示,最佳分數顯示,音效等等,都是細節。

Unity3d,Flappy,Bird

Unity3d,Flappy,Bird

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产一区二区三区视频在线 | 黄色男女视频 | 91久久另类重口变态 | 国产一区精品在线观看 | 亚洲第一综合色 | 日本高清黄色片 | 日本中文一级片 | 黄在线观看在线播放720p | 亚洲午夜精品视频 | 免费一级欧美在线观看视频 | 成人免费一区二区 | 久久精品9 | 欧美精品免费一区二区三区 | 福利一区二区三区视频在线观看 | 久久久电影电视剧免费看 | 久章草在线视频 | 日本成人二区 | 欧美成人精品一级 | 欧美一区2区三区4区公司二百 | 97久久精品一区二区三区观看 | 91短视频网址 | 青青青在线免费 | 久久免费观看一级毛片 | 国产二区三区在线播放 | 久久经典视频 | 国产人成免费爽爽爽视频 | 在线男人天堂 | 免费网址黄 | 高清国产在线 | 久久久成人精品 | 中文字幕激情视频 | 国产一级毛片高清视频 | 久久影院在线观看 | 亚洲成人午夜精品 | 91精品国产91 | 18pao国产成人免费视频 | 91色琪琪电影亚洲精品久久 | 精品在线免费播放 | 精品国产一区二区三区四区在线 | 国产成视频在线观看 | 成人短视频在线观看 |