unity3D遊戲開發十二之瘋狂的小球

來源:互聯網
上載者:User

下面我們通過一個具體的執行個體來瞭解如何使用物理引擎,該執行個體通過第三人稱視角控制遊戲中的小球對象,遊戲通過是否與鑽石碰撞來界定是否尋找到鑽石並獲得積分,獲得積分滿10分後,贏得遊戲,當小球衝出跑道時,遊戲失敗,並提示是否重新開始遊戲。


依次開啟檔案夾Assets-》object,在object檔案夾下找到Runway.fbx檔案,拖進情境中,如:


選擇Runway遊戲對象,修改Transform組件的Position(88,48.5,57),Rotation(270,350,0),Scale(1,1,1),然後選擇Main Camera對象,修改Transform組件的Position(88,60,45),Rotation(55,0,0),Scale(1,1,1),如:


然後下面我們添加一個平行光源,依次開啟功能表列中的GameObject->Create Other->Directional light,修改該對象的Transform組件的Position(87,57,58),Rotation(48,-29,1),Scale(1,1,1),如:


然後我們添加一個球體,依次開啟功能表列GameObject->Create Other->Sphere,並命名為Ball,設定它的position(84,50,48),Rotation(0,0,0),Scale(1,1,1),然後開啟功能表列Component->Physics->Rigidbody,給Ball對象添加剛體組件,如:


下面給小球添加材質檔案,如:


現在小球無法移動,所以我們得給小球綁定一個指令碼,建立一個檔案夾,命名為Scripts,在Scripts檔案夾裡建立一個C#檔案,並命名為BallControl.cs,代碼如下:

using UnityEngine;using System.Collections;public class BallControl : MonoBehaviour {//小球運動的速率public float movementSpeed=6.0f; //小球的水平運動private Vector3 horizontalMovement;//小球的前後運動private Vector3 verticalMovement;// Use this for initializationvoid Start (){}// Update is called once per framevoid Update () {//Vector3.right x zhouhorizontalMovement=Input.GetAxis("Horizontal")*Vector3.right*movementSpeed;//z zhouverticalMovement=Input.GetAxis("Vertical")*Vector3.forward*movementSpeed;//小球的運動Vector3 movement=horizontalMovement+verticalMovement;//為小球施加力rigidbody.AddForce(movement,ForceMode.Force);}}
然後綁定到Ball對象上,如:


點擊Play按鈕,我們發現可以用鍵盤控制小球移動了,如:

下面我們添加鑽石遊戲對象,將Gem.fbx檔案拖進情境中,複製10個這樣的對象,然後依次命名Gem1--Gem10,依次開啟功能表列建立一個空遊戲對象,命名Gems,然後把Gem1-10都拖進Gems對象中,如:


依次設定Gem1-Gem10對象的Transform組件屬性,Gem1的Position(78,50,55),Rotation(-90,0,0),Scale(0.27,0.27,0.27),並再Mesh Collider組件下勾選Is Trigger,設定tag為Pickup,依次設定剩下的gem對象,設好後如:


我們修改下BallControl.cs指令碼,當小球與鑽石碰撞時,讓鑽石消失,添加代碼如下:

void OnTriggerEnter(Collider other){//判斷小球是否與鑽石對象碰撞if (other.tag == "Pickup"){//銷毀對象Destroy(other.gameObject);}}

接下來,我們建立一個平面,依次開啟功能表列GameObject->Create Other->Cune,命名為GameOverTrigger,該對象用於判斷小球是否與其碰撞,如果是,表示小球衝出了跑道,遊戲失敗,我們設定Transform組件屬性,Position(90,40,55),Rotation(0,0,0),Scale(165,1,118),如:


勾選Box Collider下的Is Trigger複選框,並取消勾選Mesh Renderer組件,建立一個c#指令碼,叫GameOverTrigger.cs,代碼如下:

using UnityEngine;using System.Collections;public class GameOverTrigger : MonoBehaviour {    void OnTriggerEnter()    {        Debug.Log("over");    }}

把指令碼綁定到GameOverTrigger對象上,預覽遊戲,當小球衝出跑道後,我們列印over字樣。接下來我們在建立一個指令碼,叫CrayBallManager.cs,用來處理一些輸贏得分之類的邏輯,代碼如下:

using UnityEngine;using System.Collections;//枚舉public enum CrazyBallState { playing, won, lost };public class CrazyBallManager : MonoBehaviour{//單例public static CrazyBallManager CB;//按鈕public GUIStyle buttonStyle;//標籤public GUIStyle labelStyle;//鑽石總共的數量private int totalGems;//找到鑽石的數量private int foundGems;//遊戲的狀態private CrazyBallState gameSate;void Awake(){CB = this;foundGems = 0;gameSate = CrazyBallState.playing;totalGems = GameObject.FindGameObjectsWithTag ("Pickup").Length;//開始遊戲Time.timeScale = 1.0;}void OnGUI(){GUI.skin.label = labelStyle;GUI.skin.button = buttonStyle;//顯示找到的鑽石數量GUILayout.Label ("Found gems:"+foundGems+"/"+totalGems);if (gameSate == CrazyBallState.lost){//提示遊戲失敗GUILayout.Label("You Lost");if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,113,84),"Play again")){//重新載入情境Application.LoadLevel(Application.loadedLevel);}}else if(gameSate==CrazyBallState.won){GUILayout.Label("You won");if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,113,84),"Play again")){//重新載入情境Application.LoadLevel(Application.loadedLevel);}}}public void FoundGem(){foundGems++;if(foundGems>=totalGems){WonGame();}}public void WonGame(){//暫停遊戲Time.timeScale = 0.0;gameSate = CrazyBallState.won;}public void SetGameOver(){//暫停遊戲Time.timeScale = 0.0;gameSate = CrazyBallState.lost;}}
CrayBallManager指令檔中,每調用一次FoundGem()函數,對FoundGems變數累加一次,所以我們在BallControl.cs中修改如下代碼:

{//判斷小球是否與鑽石對象碰撞if (other.tag == "Pickup"){CrazyBallManager.CB.FoundGem();//銷毀對象Destroy(other.gameObject);}}

GameOverTrigger.cs修改如下:

 void OnTriggerEnter()    {        CrazyBallManager.CB.SetGameOver ();//

將CrayBallManager.cs指令碼綁定到Main Camera對象上,然後我們單擊Main Camera對象,單擊ButtonStyle屬性,設定Background屬性,放入一張圖片,設定FontSize為18,Alignment為Middle Center,設定Fixed Width為113,Height為84,如:


運行遊戲,如:


遊戲中的攝像機並沒有跟隨小球,造成了遊戲難以操作的現象,所以我建立一個指令碼,叫BallCamera.cs,代碼如下:

using UnityEngine;using System.Collections;public class BallCamera : MonoBehaviour{//跟隨的目標物體    public Transform target;//與目標物體的相對高度    public float relativeHeigth = 10.0f;//與目標物體的相對距離    public float zDistance = 5.0f;//阻尼速度    public float dampSpeed = 2;    void Update()    {        Vector3 newPos = target.position + new Vector3(0, relativeHeigth, -zDistance);//像彈簧一樣跟隨目標物體        transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * dampSpeed);    }}
綁定到Main Camera對象上,target為ball對象,運行遊戲,如:







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.