// Palyer.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[AddComponentMenu("MyGame/Player")]publicclassPlayer:MonoBehaviour{publicfloat m_life =3;//主角共3條命publicfloat m_speed =3;//控制主角移動速度float m_rocketRate =0.3f;//控制子彈發射頻率privatefloat m_rocketspeed =0;publicTransform m_rocket;protectedTransform m_transform;publicAudioClip m_shootClip;//聲音protectedAudioSource m_audio;//聲音源publicTransform m_explosionFX;//爆炸特效publicTransform m_explosionFX2;//爆炸特效// Start is called before the first frame updatevoidStart(){m_transform =this.transform;m_audio =this.GetComponent<AudioSource>();}privatevoidOnTriggerEnter(Collider other){if(other.tag.CompareTo("EnemyRocket")==0)//碰到敵人的子彈,生命值減一{m_life -=1;//主角掉血反饋Instantiate(m_explosionFX2, m_transform.position, Quaternion.identity);if(m_life <=0){//主角死亡爆炸特效Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);Destroy(this.gameObject);}}if(other.tag.CompareTo("Enemy")==0)//碰到敵人,生命值清零并銷毀{m_life =0;//主角死亡爆炸特效Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);Destroy(this.gameObject);}}// Update is called once per framevoidUpdate(){float moveV =0;//縱向距離float moveH =0;//橫向距離if(Input.GetKey(KeyCode.DownArrow)){moveV += m_speed * Time.deltaTime;}if(Input.GetKey(KeyCode.UpArrow)){moveV -= m_speed * Time.deltaTime;}if(Input.GetKey(KeyCode.RightArrow)){moveH -= m_speed * Time.deltaTime;}if(Input.GetKey(KeyCode.LeftArrow)){moveH += m_speed * Time.deltaTime;}m_rocketspeed -= Time.deltaTime;if(m_rocketspeed <=0){m_rocketspeed = m_rocketRate;if(Input.GetKey(KeyCode.Space)|| Input.GetMouseButton(0)){Instantiate(m_rocket,newVector3(m_transform.position.x,m_transform.position.y,m_transform.position.z +0.5f), m_transform.rotation);m_audio.PlayOneShot(m_shootClip);}//移動}m_transform.Translate(newVector3(moveH,0, moveV));}}//Rocket.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[AddComponentMenu("MyGame/Rocket")]publicclassRocket:MonoBehaviour{publicfloat m_speed =10;//設置子彈速度publicfloat m_liveTime =1;//設置子彈生存時間publicfloat m_power =1;//設置子彈威力protectedTransform m_transform;// Start is called before the first frame updatevoidStart(){m_transform =this.transform;}privatevoidOnTriggerEnter(Collider other){if(other.tag.CompareTo("Enemy")==0){Destroy(this.gameObject);}}// Update is called once per framevoidUpdate(){m_liveTime -= Time.deltaTime;if(m_liveTime <=0)Destroy(this.gameObject);m_transform.Translate(newVector3(0,0,-m_speed * Time.deltaTime));}}//Explosion.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[AddComponentMenu("MyGame/Explosion")]publicclassExplosion:MonoBehaviour{publicfloat m_life =2;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){m_life -= Time.deltaTime;if(m_life <=0){m_life =2;Destroy(this.gameObject);}}}
3、創建敵人
分別創建普通敵人和高級敵人的object對象,高級敵人可以繼承普通敵人
為敵人添加tag以及以下容器,并設定相應值,高級敵人還要在腳本容器里設置子彈的預設對象。
創鍵敵人腳本
// Enemy.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[AddComponentMenu("MyGame/Enemy")]publicclassEnemy:MonoBehaviour{publicfloat e_life =20;//敵人的生命值publicfloat m_speed =1;//敵人的移動速度publicfloat m_rotSpeed =30;//旋轉速度protectedfloat m_timer =1.5f;//變向間隔時間publicint m_point =10;//消滅一個敵人獲得的分數publicTransform m_explosionFX;//爆炸特效protectedTransform m_transform;// Start is called before the first frame updatevoidStart(){m_transform =this.transform;}// Update is called once per framevoidUpdate(){updataMove();}privatevoidOnTriggerEnter(Collider other){if(other.tag.CompareTo("PlayerRocket")==0)//如果碰到玩家子彈{Rocket rocket = other.GetComponent<Rocket>();//獲取Rocket 的腳本Componentif(rocket !=null){e_life -= rocket.m_power;if(e_life <=0){//敵人死亡時爆炸特效//Quaternion.identity,該四元數對應于“no rotation”- 對象與世界軸或父軸完全對齊Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);Destroy(this.gameObject);GameManager.instance.AddScore(m_point);//給主角加分}}}if(other.tag.CompareTo("bound")==0)//敵人飛出屏幕后自動銷毀{e_life =0;Destroy(this.gameObject);}}virtualprotectedvoidupdataMove(){m_timer -= Time.deltaTime;if(m_timer <=0){m_timer =3;//每間隔3改變一次旋轉的方向m_rotSpeed =-m_rotSpeed;}//Rotate()函數會一直旋轉,參數一表示軸,參數二表示旋轉角度,參數三表示旋轉參考系m_transform.Rotate(Vector3.up, m_rotSpeed * Time.deltaTime, Space.World);//旋轉方向m_transform.Translate(newVector3(0,0,-m_speed * Time.deltaTime));//前進}}//SuperEnemy.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[AddComponentMenu("MyGame/SuperEnemy")]publicclassSuperEnemy:Enemy{publicTransform m_rocket;protectedfloat m_fireTime =2;protectedTransform m_player;voidAwake()//在游戲全體實例化時執行一次,并先于stat 方法{GameObject obj = GameObject.FindGameObjectWithTag("player");if(obj !=null){m_player = obj.transform;}}// Start is called before the first frame updateprotectedoverridevoidupdataMove(){m_fireTime -= Time.deltaTime;if(m_fireTime <=0){m_fireTime =2;if(m_player !=null){Vector3 relativePos = m_transform.position - m_player.position;Instantiate(m_rocket, m_transform.position, Quaternion.LookRotation(relativePos));}}//前進m_transform.Translate(newVector3(0,0,-m_speed * Time.deltaTime));}privatevoidOnTriggerEnter(Collider other){if(other.tag.CompareTo("PlayerRocket")==0)//如果碰到玩家子彈{Rocket rocket = other.GetComponent<Rocket>();//獲取Rocket 的腳本Componentif(rocket !=null){e_life -= rocket.m_power;if(e_life <=0){//敵人死亡時爆炸特效Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);Destroy(this.gameObject);GameManager.instance.AddScore(m_point);//給主角加分}}}if(other.tag.CompareTo("bound")==0)//敵人飛出屏幕后自動銷毀{e_life =0;Destroy(this.gameObject);}}}
4、創建敵人生成器
在地圖的上方的指定地點,放置幾個空對象,用于生成敵人
敵人生成器有兩種類型,分別生成不同類型的敵人,需要在腳本容器中指定敵人
為敵人生成器創建腳本
// EnemySpawn.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[AddComponentMenu("MyGame/EnemySpawn")]publicclassEnemySpawn:MonoBehaviour{publicTransform m_enemy;//敵人的prefabpublicfloat m_timer =5;//生成敵人的間隔時間protectedTransform m_transform;// Start is called before the first frame updatevoidStart(){m_transform =this.transform;}// Update is called once per framevoidUpdate(){m_timer -= Time.deltaTime;if(m_timer <=0){m_timer =5+ Random.value*15.0f;//產生5 - 15之間的隨機數// Quaternion.identity就是指Quaternion(0,0,0,0)Instantiate(m_enemy, m_transform.position, Quaternion.identity);}}//用一幅圖填充空對象,在控制臺方便知道生成器的位置,游戲中看不到voidOnDrawGizmos(){Gizmos.DrawIcon(transform.position,"item.png",true);}}
5、創建游戲管理器
游戲管理器是一個空對象,有且只能有一個,用于控制游戲界面,以及控制游戲開始和退出。
創建一個空對象,命名為GameManager,為其添加腳本
為其添加Audio Source容器,并在腳本容器中指定背景音樂文件
// GameManager.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[AddComponentMenu("MyGame/GameManager")]publicclassGameManager:MonoBehaviour{publicstaticGameManager instance;publicfloat m_score =0;publicstaticint m_hiscore =0;publicAudioClip m_musicClip;//指定音樂片段protectedPlayer m_player;protectedAudioSource m_audio;privatevoidAwake(){instance =this;}// Start is called before the first frame updatevoidStart(){m_audio =this.GetComponent<AudioSource>();//獲取主角GameObject obj = GameObject.FindGameObjectWithTag("player");if(obj !=null){m_player = obj.GetComponent<Player>();}}// Update is called once per framevoidUpdate(){//循環播放背景音樂if(!m_audio.isPlaying){m_audio.clip = m_musicClip;m_audio.Play();}//暫停游戲if(Time.timeScale >0&& Input.GetKeyDown(KeyCode.Escape)){Time.timeScale =0;}}[System.Obsolete]privatevoidOnGUI(){//暫停游戲if(Time.timeScale ==0){//繼續游戲按鈕if(GUI.Button(newRect(Screen.width *0.5f-50,Screen.height *0.4f,100,30),"繼續游戲")){Time.timeScale =1;}//退出游戲按鈕if(GUI.Button(newRect(Screen.width *0.5f-50, Screen.height *0.6f,100,30),"退出游戲")){Application.Quit();//只有當工程打包編譯后的程序使用Application.Quit()才奏效}}int life =0;if(m_player !=null){life =(int)m_player.m_life;//獲取主角的生命值}else//主角被銷毀,即游戲失敗{//放大字體GUI.skin.label.fontSize =50;//顯示游戲失敗GUI.skin.label.alignment = TextAnchor.LowerCenter;GUI.Label(newRect(0, Screen.height *0.2f, Screen.width,60),"游戲失敗");GUI.skin.label.fontSize =20;//顯示再來一次按鈕if(GUI.Button(newRect(Screen.width *0.5f-50, Screen.height *0.5f,100,30),"再來一次")){//讀取當前關卡Application.LoadLevel(Application.loadedLevelName);}}GUI.skin.label.fontSize =15;//顯示主角生命GUI.Label(newRect(5,5,100,30),"裝甲:"+ life);//顯示最高分GUI.skin.label.alignment = TextAnchor.LowerCenter;GUI.Label(newRect(0,5, Screen.width,30),"最高分:"+ m_hiscore);//顯示當前得分GUI.Label(newRect(0,25, Screen.width,30),"得分:"+ m_score);}publicvoidAddScore(int point){m_score += point;// 更高分記錄if(m_hiscore < m_score){m_hiscore =(int)m_score;}}}
//TitleSceen.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[AddComponentMenu("MyGame/TitleScreen")]publicclassTitleSceen:MonoBehaviour{// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){}[System.Obsolete]privatevoidOnGUI(){GUI.skin.label.fontSize =48;GUI.skin.label.alignment = TextAnchor.LowerCenter;//UI中心對齊GUI.Label(newRect(0,30, Screen.width,100),"太空大戰");//開始游戲按鈕if(GUI.Button(newRect(Screen.width *0.5f-100, Screen.height *0.7f,200,30),"開始游戲")){Application.LoadLevel("level1");}}}