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

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");
					}
				else
					Application.LoadLevel("lose");

修改timeRemainDisplay中的代码:

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


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

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



修改最高纪录脚本:

#pragma strict
private 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");}
	else	Application.LoadLevel("win");
	
	}
	
	if(Input.GetKeyDown(KeyCode.Q))
		PlayerPrefs.SetInt("highscore",100);
	if(leftTime<50.0)
		enemy_b1.SetActive(true);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值