您好,登錄后才能下訂單哦!
本文實例為大家分享了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; } }
此外還有分數顯示,最佳分數顯示,音效等等,都是細節。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。