1.創建一個Unity3D項目
2.將Camera設置為正交投影
游戲的輸出畫面是通過攝像機所觀察的場景來實現的,將場景呈現到2D的計算機屏幕上具有兩種不同的投影方式:透視投影和正交投影,默認狀態下是透視投影.
透視投影
正交投影
3.添加相關對象模型
移動攝像機 物體,燈光到 如下效果
添加游戲物體移動超出邊框的控制
using UnityEngine; using System.Collections; public class Player:MonoBehaviour { public float playerSpeed; // Use this for initialization void Start () { } // Update is called once per frame void Update () { float amtToMove = Input.GetAxis("Horizontal")*Time.deltaTime*playerSpeed; gameObject.transform.Translate(Vector3.right*amtToMove); if(transform.position.x<-7.5){ //如果方塊移動超出游戲窗體右邊,方框將從左邊窗口進入 transform.position = new Vector3(5.25f,transform.position.y,transform.position.z); } if(transform.position.x>5.25){ //如果方塊移動超出游戲窗體右邊,方框將從左邊窗口進入 transform.position = new Vector3(-7.5f,transform.position.y,transform.position.z); } } }
4.創建炮彈[create-Capsule]
根據需要調整炮彈的大小
選擇炮彈模型,單機Component菜單,選擇Physics-RigidBody,將炮彈設置為剛體,以便后面實現碰撞檢測.
為炮彈添加移動操作腳本
using UnityEngine; using System.Collections; public class bulletTile : MonoBehaviour { public float bulletSpeed ; private Transform mytransform; // Use this for initialization void Start () { mytransform = gameObject.transform; } // Update is called once per frame void Update () { //定義炮彈移動速度 float amtToMove = bulletSpeed * Time.deltaTime; //讓炮彈垂直向上移動 mytransform.Translate(Vector3.up * amtToMove); //如果炮彈移動超出游戲場景則銷毀炮彈 if(mytransform.position.y>5.15){ Destroy(this.gameObject); //銷毀當前對象 } } }
對象重用
在Project中創建一個Prefab對象,然后將要重用的對象模型拖動到該Prefab對象上,即可實現模型對象的重用.
發射子彈的實現
在Player腳本中創建Prefab可重用對象實例
using UnityEngine; using System.Collections; public class Player:MonoBehaviour { public float playerSpeed; public GameObject bulletPrefab; // Use this for initialization void Start () { } // Update is called once per frame void Update () { float amtToMove = Input.GetAxis("Horizontal")*Time.deltaTime*playerSpeed; gameObject.transform.Translate(Vector3.right*amtToMove); if(transform.position.x<-7.5){ //如果方塊移動超出游戲窗體右邊,方框將從左邊窗口進入 transform.position = new Vector3(5.25f,transform.position.y,transform.position.z); } if(transform.position.x>5.25){ //如果方塊移動超出游戲窗體右邊,方框將從左邊窗口進入 transform.position = new Vector3(-7.5f,transform.position.y,transform.position.z); } //獲取發射器位置 發射器的正上方 Vector3 position = new Vector3(transform.position.x,transform.position.y+transform.localScale.y/2.0f,transform.position.z); //按下空格鍵發射子彈 if(Input.GetKeyDown("space")){ //實例化一個炮彈對象 Instantiate(bulletPrefab,position,Quaternion.identity); } } }
拖動表示將屬性實例化,因為我們腳本中定義的是public屬性,在這里我們可以手動為public屬性賦值,此時運行程序,用左右鍵控制發射器的移動,用space發射子彈.
應用實例:
游戲中聲音的添加與控制
支持的聲音文件: *.aiff, *.wav, *.mp3, *.ogg
.AIFF
轉換為無壓縮音頻導入,最適合短音效果。可以在編輯器中按需求壓縮。
.WAV
轉換為無壓縮音頻導入,最適合短音效果。可以在編輯器中按需求壓縮
.MP3
轉換成Ogg格式導入,最適合較長的音樂曲目。
.OGG
壓縮音頻格式(與iPhone設備和某些Android設備不兼容),最適合較長的音樂曲目。
添加聲音控制按鈕,
void OnGUI(){ GUI.Button (new Rect (10,10,100,35), "播放音樂"); GUI.Button (new Rect (10,60,100,35), "暫停播放"); GUI.Button (new Rect (10,110,100,35), "停止音樂"); }
為按鈕添加事件
void OnGUI(){ if(GUI.Button (new Rect (10,10,100,35), "播放音樂")){ gameObject.audio.Play(); } if(GUI.Button (new Rect (10,60,100,35), "暫停播放")){ gameObject.audio.Pause(). } if(GUI.Button (new Rect (10,110,100,35), "停止音樂")){ gameObject.audio.Stop(); } }
5.為發射炮彈添加聲音
選中bulletPrefab,單機窗體中的Component-Audio-Audion Source