Unity3D太空大战.docx
《Unity3D太空大战.docx》由会员分享,可在线阅读,更多相关《Unity3D太空大战.docx(14页珍藏版)》请在冰豆网上搜索。
Unity3D太空大战
Unity3D
太空大战源码
(1)Enemy的源码:
usingUnityEngine;
usingSystem.Collections;
[AddComponentMenu("MyGame/Enemy")]
publicclassEnemy:
MonoBehaviour{
//速度
publicfloatm_speed=1;
//生命
publicfloatm_life=10;
//旋转速度
protectedfloatm_rotSpeed=30;
//变向间隔时间
protectedfloatm_timer=1.5f;
protectedTransformm_transform;
publicTransformm_explosionFX;
publicintm_point=10;
//Usethisforinitialization
voidStart(){
m_transform=this.transform;
}
//Updateiscalledonceperframe
voidUpdate(){
UpdateMove();
}
protectedvirtualvoidUpdateMove()
{
m_timer-=Time.deltaTime;
if(m_timer<=0)
{
m_timer=3;
//改变旋转方向
m_rotSpeed=-m_rotSpeed;
}
//旋转方向
m_transform.Rotate(Vector3.up,m_rotSpeed*Time.deltaTime,Space.World);
//前进
m_transform.Translate(newVector3(0,0,-m_speed*Time.deltaTime));
}
voidOnTriggerEnter(Colliderother)
{
if(other.tag.CompareTo("PlayerRocket")==0)
{
Rocketrocket=other.GetComponent();
if(rocket!
=null)
{
m_life-=rocket.m_power;
if(m_life<=0)
{
GameManager.Instance.AddScore(m_point);
Instantiate(m_explosionFX,m_transform.position,Quaternion.identity);
Destroy(this.gameObject);
}
}
}
elseif(other.tag.CompareTo("Player")==0)
{
m_life=0;
Instantiate(m_explosionFX,m_transform.position,Quaternion.identity);
Destroy(this.gameObject);
}
if(other.tag.CompareTo("bound")==0)
{
m_life=0;
Destroy(this.gameObject);
}
}
}
(2)EnemyRocket的源码:
usingUnityEngine;
usingSystem.Collections;
[AddComponentMenu("MyGame/EnemyRocket")]
publicclassEnemyRocket:
Rocket
{
voidOnTriggerEnter(Colliderother)
{
if(other.tag.CompareTo("Player")!
=0)
return;
Destroy(this.gameObject);
}
}
(3)EnemySpawn敌人生成器:
usingUnityEngine;
usingSystem.Collections;
[AddComponentMenu("MyGame/EnemySpawn")]
publicclassEnemySpawn:
MonoBehaviour
{
//敌人的Prefab
publicTransformm_enemy;
//生成敌人的时间间隔
protectedfloatm_timer=5;
protectedTransformm_transform;
//Usethisforinitialization
voidStart(){
m_transform=this.transform;
}
//Updateiscalledonceperframe
voidUpdate(){
m_timer-=Time.deltaTime;
if(m_timer<=0)
{
m_timer=Random.value*15.0f;
if(m_timer<5)
m_timer=5;
Instantiate(m_enemy,m_transform.position,Quaternion.identity);
}
}
voidOnDrawGizmos()
{
Gizmos.DrawIcon(transform.position,"item.png",true);
}
}
(4)GameMannager图形界面:
usingUnityEngine;
usingSystem.Collections;
[AddComponentMenu("MyGame/GameManager")]
publicclassGameManager:
MonoBehaviour{
publicstaticGameManagerInstance;
//得分
publicintm_score=0;
//纪录
publicstaticintm_hiscore=0;
//主角
protectedPlayerm_player;
//背景音乐
publicAudioClipm_musicClip;
//声音源
protectedAudioSourcem_Audio;
voidAwake()
{
Instance=this;
}
//Usethisforinitialization
voidStart(){
m_Audio=this.audio;
//获取主角
GameObjectobj=GameObject.FindGameObjectWithTag("Player");
if(obj!
=null)
{
m_player=obj.GetComponent();
}
}
//Updateiscalledonceperframe
voidUpdate(){
//循环播放背景音乐
if(!
m_Audio.isPlaying)
{
m_Audio.clip=m_musicClip;
m_Audio.Play();
}
//暂停游戏
if(Time.timeScale>0&&Input.GetKeyDown(KeyCode.Escape))
{
Time.timeScale=0;
}
}
voidOnGUI()
{
//游戏暂停
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();
}
}
intlife=0;
if(m_player!
=null)
{
//获得主角的生命值
life=(int)m_player.m_life;
}
else//gameover
{
//放大字体
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(intpoint)
{
m_score+=point;
//更新高分纪录
if(m_hiscorem_hiscore=m_score;
}
}
(5)Player主飞船的源码:
usingUnityEngine;
usingSystem.Collections;
[AddComponentMenu("MyGame/Player")]
publicclassPlayer:
MonoBehaviour{
//速度
publicfloatm_speed=1;
//生命
publicfloatm_life=3;
//子弹prefab
publicTransformm_rocket;
protectedTransformm_transform;
//发射子弹频率
floatm_rocketRate=0;
//声音
publicAudioClipm_shootClip;
//声音源
protectedAudioSourcem_audio;
//爆炸特效
publicTransformm_explosionFX;
//Usethisforinitialization
voidStart(){
m_transform=this.transform;
m_audio=this.audio;
}
//Updateiscalledonceperframe
voidUpdate(){
//纵向移动距离
floatmovev=0;
//水平移动距离
floatmoveh=0;
//按上键
if(Input.GetKey(KeyCode.UpArrow))
{
movev-=m_speed*Time.deltaTime;
}
//按下键
if(Input.GetKey(KeyCode.DownArrow))
{
movev+=m_speed*Time.deltaTime;
}
//按左键
if(Input.GetKey(KeyCode.LeftArrow))
{
moveh+=m_speed*Time.deltaTime;
}
//按右键
if(Input.GetKey(KeyCode.RightArrow))
{
moveh-=m_speed*Time.deltaTime;
}
//移动
this.m_transform.Translate(newVector3(moveh,0,movev));
m_rocketRate-=Time.deltaTime;
if(m_rocketRate<=0)
{
m_rocketRate=0.1f;
if(Input.GetKey(KeyCode.Space)||Input.GetMouseButton(0))
{
Instantiate(m_rocket,m_transform.position,m_transform.rotation);
//播放射击声音
m_audio.PlayOneShot(m_shootClip);
}
}
}
voidOnTriggerEnter(Colliderother)
{
if(other.tag.CompareTo("PlayerRocket")!
=0)
{
m_life-=1;
if(m_life<=0)
{
//爆炸特效
Instantiate(m_explosionFX,m_transform.position,Quaternion.identity);
Destroy(this.gameObject);
}
}
}
}
(6)Rocket子弹的源码:
usingUnityEngine;
usingSystem.Collections;
[AddComponentMenu("MyGame/Rocket")]
publicclassRocket:
MonoBehaviour{
//子弹飞行速度
publicfloatm_speed=10;
//生存时间
publicfloatm_liveTime=1;
//威力
publicfloatm_power=1.0f;
protectedTransformm_trasform;
//Usethisforinitialization
voidStart(){
m_trasform=this.transform;
}
//Updateiscalledonceperframe
voidUpdate(){
m_liveTime-=Time.deltaTime;
if(m_liveTime<=0)
Destroy(this.gameObject);
m_trasform.Translate(newVector3(0,0,-m_speed*Time.deltaTime));
}
voidOnTriggerEnter(Colliderother)
{
if(other.tag.CompareTo("Enemy")!
=0)
return;
Destroy(this.gameObject);
}
}
(7)SuperEnemy源码:
usingUnityEngine;
usingSystem.Collections;
[AddComponentMenu("MyGame/SuperEnemy")]
publicclassSuperEnemy:
Enemy{
publicTransformm_rocket;
protectedfloatm_fireTimer=2;
protectedTransformm_player;
voidAwake()
{
GameObjectobj=GameObject.FindGameObjectWithTag("Player");
if(obj!
=null)
{
m_player=obj.transform;
}
}
protectedoverridevoidUpdateMove()
{
m_fireTimer-=Time.deltaTime;
if(m_fireTimer<=0)
{
m_fireTimer=2;
if(m_player!
=null)
{
Vector3relativePos=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));
}
}
(8)TitleScreen的源码:
usingUnityEngine;
usingSystem.Collections;
[AddComponentMenu("MyGame/TitleScreen")]
publicclassTitleScreen:
MonoBehaviour
{
voidOnGUI()
{
//文字大小
GUI.skin.label.fontSize=48;
//UI中心对齐
GUI.skin.label.alignment=TextAnchor.LowerCenter;
//显示标题
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");
}
}
}