1. 创建其他场景
复制startController的脚本代码,创建一个新场景--另存为命名--创建新脚本--把代码复制上--把脚本拖拽到主摄像机上--拖拽图片--完成
2. 完善场景代码
win场景:之前的分数,生命数,倒计时时间等进行初始化。
在winController和loseController添加start函数:
复制代码
1
2
3
4
5function Start(){ RockController.score:int=0; RockController.lives=3; timeRemainDisplay.leftTime=100; }
3.添加调用场景代码
修改陨石控制的OnTrigger函数:
复制代码
在文件-BuildSetting里勾选上相应场景。
1
2
3
4
5
6
7
8
9
10if(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); }
在timeRemainDisplay里的Update函数里修改:
复制代码
1
2
3
4if(leftTime<1.0) { leftTime=0; Application.LoadLevel("win"); }
4. 随机降落多种陨石
准备好多种陨石图片。
在岩石脚本中申请图片数组:
复制代码
1var ysNumbers:Texture[];
在属性界面修改size,并将所有陨石图片拖拽过来。
修改OnTiggerEnter函数,在两个if语句的transform修改前都添加:
复制代码
Mathf.RoundToInt 取整,Random.Range产生0.0~3.0之间的随机数。
1renderer.material.mainTexture=ysNumbers[Mathf.RoundToInt(Random.Range(0.0,3.0))];
5. 最高计分功能
Unity中有一个玩家偏好类
修改陨石控制脚本中if(lives<1)代码,添加:
复制代码
1
2
3
4
5
6if(score>PlayerPrefs.GetInt("highscore")) { Application.LoadLevel("score"); } else Application.LoadLevel("lose");
修改timeRemainDisplay中的代码:
复制代码
1
2
3
4
5
6if(leftTime<1.0) { leftTime=0; if(RockController.score>PlayerPrefs.GetInt("highscore")) {Application.LoadLevel("score");} else Application.LoadLevel("win"); }
在编辑菜单中找到projectSetting--player 设置公司名游戏名ICON等。
修改最高纪录脚本:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#pragma strict private var xm:string="Genius"; function Start () { rockController.score=0; rockController.lives=3; timeRemainDisplay.leftTime=100; } private var intext:String ="Instruction:nn 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代码:
复制代码
1
2
3
4
5
6
7
8
9if(RockController.score>PlayerPrefs.GetInt("highscore")) { PlayerPrefs.SetInt("highscore",rockController.score); Application.LoadLevel("score");} else Application.LoadLevel("win"); } if(Input.GetKeyDown(KeyCode.Q)) PlayerPrefs.SetInt("highscore",100); if(leftTime<50.0) enemy_b1.SetActive(true);
最后
以上就是贪玩猫咪最近收集整理的关于Unity学习之路(一)太空大战类游戏制作4的全部内容,更多相关Unity学习之路(一)太空大战类游戏制作4内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复