我是靠谱客的博主 贪玩猫咪,这篇文章主要介绍Unity学习之路(一)太空大战类游戏制作4,现在分享给大家,希望可以做个参考。

1. 创建其他场景

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


2. 完善场景代码

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

在winController和loseController添加start函数:

复制代码
1
2
3
4
5
function Start(){ RockController.score:int=0; RockController.lives=3; timeRemainDisplay.leftTime=100; }


3.添加调用场景代码

修改陨石控制的OnTrigger函数:

复制代码
1
2
3
4
5
6
7
8
9
10
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函数里修改:

复制代码
1
2
3
4
if(leftTime<1.0) { leftTime=0; Application.LoadLevel("win"); }


4. 随机降落多种陨石

准备好多种陨石图片。

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

复制代码
1
var ysNumbers:Texture[];

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


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

复制代码
1
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)代码,添加:

复制代码
1
2
3
4
5
6
if(score>PlayerPrefs.GetInt("highscore")) { Application.LoadLevel("score"); } else Application.LoadLevel("lose");

修改timeRemainDisplay中的代码:

复制代码
1
2
3
4
5
6
if(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
9
if(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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部