Unity学习之路(一)太空大战类游戏制作4

来源:互联网 发布:cydia怎么添加软件源 编辑:程序博客网 时间:2024/05/16 01:12

1. 创建其他场景

复制startController的脚本代码,创建一个新场景--另存为命名--创建新脚本--把代码复制上--把脚本拖拽到主摄像机上--拖拽图片--完成


2. 完善场景代码

win场景:之前的分数,生命数,倒计时时间等进行初始化。

在winController和loseController添加start函数:

function Start(){RockController.score:int=0;RockController.lives=3;timeRemainDisplay.leftTime=100;}


3.添加调用场景代码

修改陨石控制的OnTrigger函数:

if(other.tag=="Player"){Instantiate(explosionPlayer_b1,transform.position,transform.rotation);lives--;if(lives==0){Application.LoadLevel("lose");}transform.position=new Vector3(Random.Range(-2.6,2.6),3.5,0);}
在文件-BuildSetting里勾选上相应场景。

在timeRemainDisplay里的Update函数里修改:

if(leftTime<1.0){leftTime=0;Application.LoadLevel("win");}


4. 随机降落多种陨石

准备好多种陨石图片。

在岩石脚本中申请图片数组:

var ysNumbers:Texture[];

在属性界面修改size,并将所有陨石图片拖拽过来。


修改OnTiggerEnter函数,在两个if语句的transform修改前都添加:

renderer.material.mainTexture=ysNumbers[Mathf.RoundToInt(Random.Range(0.0,3.0))];
Mathf.RoundToInt 取整,Random.Range产生0.0~3.0之间的随机数。



5. 最高计分功能

Unity中有一个玩家偏好类

修改陨石控制脚本中if(lives<1)代码,添加:

if(score>PlayerPrefs.GetInt("highscore")){Application.LoadLevel("score");}elseApplication.LoadLevel("lose");

修改timeRemainDisplay中的代码:

if(leftTime<1.0){leftTime=0;if(RockController.score>PlayerPrefs.GetInt("highscore")){Application.LoadLevel("score");}elseApplication.LoadLevel("win");}


别忘记在游戏发布中把所有场景都拖过来。

在编辑菜单中找到projectSetting--player 设置公司名游戏名ICON等。



修改最高纪录脚本:

#pragma strictprivate var xm:string="Genius";function Start () {rockController.score=0;rockController.lives=3;timeRemainDisplay.leftTime=100;}private var intext:String ="Instruction:\n\n Press left and Right arrow To Move .\n Press Space to fire.";var scoreTexture:Texture;function OnGUI(){GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),scoreTexture);GUI.Label(new Rect(10,10,250,200),intext);xm=GUI.TextField(Rect(460,525,200,20),xm,25);if(GUI.Button(Rect(410,560,100,20),"Continue...")){PlayerPrefs.SetString("sir",xm);Application.LoadLevel("level");}}


修改部分timeRemainDisplay中的update代码:

if(RockController.score>PlayerPrefs.GetInt("highscore")){PlayerPrefs.SetInt("highscore",rockController.score);Application.LoadLevel("score");}elseApplication.LoadLevel("win");}if(Input.GetKeyDown(KeyCode.Q))PlayerPrefs.SetInt("highscore",100);if(leftTime<50.0)enemy_b1.SetActive(true);


0 0
原创粉丝点击