unity 3d 三款游戏完整入门进阶必备代码.docx

上传人:b****1 文档编号:12712218 上传时间:2023-04-21 格式:DOCX 页数:26 大小:22.27KB
下载 相关 举报
unity 3d 三款游戏完整入门进阶必备代码.docx_第1页
第1页 / 共26页
unity 3d 三款游戏完整入门进阶必备代码.docx_第2页
第2页 / 共26页
unity 3d 三款游戏完整入门进阶必备代码.docx_第3页
第3页 / 共26页
unity 3d 三款游戏完整入门进阶必备代码.docx_第4页
第4页 / 共26页
unity 3d 三款游戏完整入门进阶必备代码.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

unity 3d 三款游戏完整入门进阶必备代码.docx

《unity 3d 三款游戏完整入门进阶必备代码.docx》由会员分享,可在线阅读,更多相关《unity 3d 三款游戏完整入门进阶必备代码.docx(26页珍藏版)》请在冰豆网上搜索。

unity 3d 三款游戏完整入门进阶必备代码.docx

unity3d三款游戏完整入门进阶必备代码

一.太空陨石大战

//******控制太空背景向下移动的游戏脚本******

usingUnityEngine;

usingSystem.Collections;

publicclassBackgroundContoller:

MonoBehaviour{

publicfloatspeed=1.0f;//设置背景向下移动的速度

//Usethisforinitialization

voidStart(){}//Updateiscalledonceperframe

voidUpdate(){

transform.Translate(0,-speed*Time.deltaTime,0);//实现背景向下移动

if(transform.position.y<-5.17f)//判断背景是否移出主摄像机照射区域(即游戏屏幕)transform.position=newVector3(0,12,1);//若移出,则重新设置游戏背景Y轴位置如0,6,12,//以便实现无缝连接使游戏背景连续播放}}

usingUnityEngine;

usingSystem.Collections;

publicclassExplosionController:

MonoBehaviour{

publicintindex=0;

publicintframeNumber=7;//定义帧数,爆炸动画由7帧组成

floatframeRate=0;//定义帧速率

floatmyTime=0;

intmyIndex=0;

//Usethisforinitialization

voidStart(){

frameRate=1.0f/frameNumber;//帧速率,帧数framenumber=7}

//Updateiscalledonceperframe

voidUpdate(){

myTime+=Time.deltaTime;

//统计爆炸效果动画累计播放的时间

myIndex=(int)(myTime*frameNumber);

//计算“我的索引值”,使用(int)转成整形:

0,2,3,4,5,6

//Debug.Log("(int)(myTime*frameNumber)");

//Debug.Log((int)(myTime*frameNumber));

index=myIndex%frameNumber;

//除以7求余数得索引:

0,2,3,4,5,6

//Debug.Log(myIndex%frameNumber);

render.material.mainTextureScale=newVector2(frameRate,1);

//通过render.material.mainTextureScale设置tiling帧宽属性

render.material.mainTextureOffset=newVector2(index*frameRate,0);

//通过render.material.mainTextureOffset设置帧偏移量

if(index==frameNumber-1)//如果动画帧数播放完等于6,即索引等于6,则销毁动画

Destroy(gameObject);}}

usingUnityEngine;

usingSystem.Collections;

publicclassLoseController:

MonoBehaviour{

publicTextureloseTexture;

//Usethisforinitialization

voidStart(){

RockController.score=0;

RockController.lives=3;

TimeRemainDisplay.leftTime=100;}//Updateiscalledonceperframe

voidOnGUI(){

GUI.DrawTexture(newRect(0,0,Screen.width,Screen.height),loseTexture);

if(Input.anyKeyDown){Application.LoadLevel("Level");}}}//****控制飞机的游戏脚本****

usingUnityEngine;

usingSystem.Collections;

publicclassPlayerController:

MonoBehaviour{

publicfloatspeed=2.0f;//定义飞机在X轴上的移动速度

publicGameObjectprojectile;//定义一个共有子弹对象

//Usethisforinitialization

voidStart(){}//Updateiscalledonceperframe

voidUpdate(){

transform.Translate(speed*Input.GetAxis("Horizontal")*Time.deltaTime,0,0);//实现飞机在X轴上,按下左右键时可水平移动,speed一定要乘上

if(Input.GetKeyDown(KeyCode.Space))

//判断是否按下xx

Instantiate(projectile,transform.position,transform.rotation);//按下空格键时,发射炮弹对象projectile,炮弹发射位置飞机中部transform。

position}//炮弹发射角度transform。

rotation}//******控制子弹的游戏脚本******

usingUnityEngine;

usingSystem.Collections;

publicclassProjectileController:

MonoBehaviour{

publicfloatspeed=5.0f;//定义炮弹发射速度

//Usethisforinitialization

voidStart(){}//Updateiscalledonceperframe

voidUpdate(){

transform.Translate(0,speed*Time.deltaTime,0);//实现子弹在屏幕上发射时,在Y轴上移动if(transform.position.y>2.14f)//判断子弹位置是否移出游戏屏幕Destroy(gameObject);//若移出,销毁子弹对象}}

//******控制陨石的游戏脚本******

usingUnityEngine;

usingSystem.Collections;

publicclassRockController:

MonoBehaviour{

publicfloatspeed=2.0f;//定义陨石的下落速度

publicGameObjectexplosionPlayer;//定义爆炸动画对象

publicGameObjectexplosionEnemy;//定义爆炸动画对象

publicstaticintscore=0;//定义击落陨石获得的积分

publicstaticintlives=3;//定义玩家生命

publicstaticinthighScore=0;//设置最高分的存储变量

//Usethisforinitialization

voidStart(){

highScore=PlayerPrefs.GetInt("HighScore");//调用PlayerPrefs类中GetInt()方法读取保存在本地的变量highScore}//Updateiscalledonceperframe

voidUpdate(){

transform.Translate(0,-speed*Time.deltaTime,0);//实现陨石在Y方向上的下落运动if(transform.position.y<-2.56f)//判断陨石在Y轴向的位置,是否离开游戏界面下边界transform.position=newVector3(Random.Range(-2.2f,2.2f),2.5f,0);//若离开则在上界面随机重新生成陨石}voidOnTriggerEnter(Colliderother)//碰撞检测{if(other.tag=="projectile")//子弹与陨石的碰撞检测,判断碰撞的标签是否为子弹{

score+=100;

Debug.Log("score+=100;");

Instantiate(explosionEnemy,transform.position,transform.rotation);

//调用炮弹碰撞陨石所发生的爆炸效果

transform.position=newVector3(Random.Range(-2.2f,2.2f),2.5f,0);

//重置陨石的的代码,在指定位置重新生成陨石

Destroy(other.gameObject);//销毁子弹}if(other.tag=="player")//飞机与陨石的碰撞检测{lives--;

if(lives==0){//如果玩家飞机生命值为零,转换进入输家场景if(RockController.score>PlayerPrefs.GetInt("HighScore")){

//若当前获得分数大于之前本地保存的最高分

highScore=RockController.score;//保存最高分变量PlayerPrefs.SetInt("HighScore",RockController.score);//更新最高分}else

highScore=PlayerPrefs.GetInt("HighScore");//否则继续保存本地最高分

Application.LoadLevel("Lose");}Instantiate(explosionPlayer,transform.position,transform.rotation);//调用飞机与陨石碰撞的爆炸效果

transform.position=newVector3(Random.Range(-2.2f,2.2f),2.5f,0);//重置陨石的的代码,在指定位置重新生成陨石

//Destroy(other.gameObject);//销毁飞机,所以将其注释掉,不销毁飞机;Destroy(gameObject)是销毁陨石的}}

voidOnGUI(){GUI.Label(newRect(10,10,120,120),"score:

"+score.ToString());

GUI.Label(newRect(10,30,60,20),"Lives:

"+lives.ToString());

GUI.Label(newRect(10,50,120,20),"highscore:

"+highScore.ToString());

//显示最高分}}

//*********游戏开始场景控制代码**********

usingUnityEngine;

usingSystem.Collections;

publicclassStartController:

MonoBehaviour{

privatestringinstructionText="Instruction:

\n\nPressleftandrightarrowtomove.\nPressSpacetofire.";//定义简单的游戏操作说明

publicTexturestartTexture;//定义关联start场景的变量

voidOnGUI(){

GUI.DrawTexture(newRect(0,0,Screen.width,Screen.height),startTexture);//绘制开始start场景区域,并添加关联的场景

GUI.Label(newRect(10,10,250,200),instructionText);//绘制游戏简介内容

if(Input.anyKeyDown){Application.LoadLevel("Level");//按下任意键,加载场景1

Debug.Log("Level");}}}//*********个性化游戏倒计时控制代码******

usingUnityEngine;

usingSystem.Collections;

publicclassTimeRemainDisplay:

MonoBehaviour{

publicTexture[]timeNumbers;//定义一个贴图数组,用于存放数字图片0,1,2...9publicstaticintleftTime=100;//定义游戏倒计时时间为100秒,并用于存放游戏剩余时间

floatmyTime;

//Usethisforinitialization

voidStart(){}//Updateiscalledonceperframe

voidUpdate(){

myTime+=Time.deltaTime;//游戏花费时间的累计,每当累计到1秒时,倒计时leftTime减1秒

if(myTime>1){leftTime--;

myTime=0;}if(leftTime==0)//若游戏倒计时结束,则转入玩家胜利的Win场景{

if(RockController.score>PlayerPrefs.GetInt("HighScore")){

RockController.highScore=RockController.score;

PlayerPrefs.SetInt("HighScore",RockController.score);

}else

RockController.highScore=PlayerPrefs.GetInt("HighScore");

Application.LoadLevel("Win");}}

voidOnGUI(){for(inti=0;i

GUI.DrawTexture(new

Rect(300+i*32,20,32,45),timeNumbers[System.Int32.Parse(leftTime.ToString()[i].ToString())]);}}

usingUnityEngine;

usingSystem.Collections;

publicclassWinController:

MonoBehaviour{

publicTexturewinTexture;

//Usethisforinitialization

voidStart(){

RockController.score=0;

RockController.lives=3;

TimeRemainDisplay.leftTime=100;}//Updateiscalledonceperframe

voidOnGUI(){

GUI.DrawTexture(newRect(0,0,Screen.width,Screen.height),winTexture);

if(Input.anyKeyDown)

Application.LoadLevel("Level");}}

二.坦克大战游戏:

usingUnityEngine;

usingSystem.Collections;

publicclassBackgroundController:

MonoBehaviour{

publicfloatspeed=2.0f;//声明背景向左移动的速度privateboolchange=false;//为背景的循环移动声明一个change变量,用于标识背景是否需要移动

//Usethisforinitialization

voidStart(){}//Updateiscalledonceperframe

voidUpdate(){

transform.Translate(Vector3.left*speed*Time.deltaTime);

if(transform.position.x<-14.4){transform.position=newVector3(15.5f,transform.position.y,transform.position.z);change=true;}if(transform.position.x<0.5&&change){transform.position=newVector3(0.5f,transform.position.y,transform.position.z);change=false;}}}usingUnityEngine;

usingSystem.Collections;

publicclassAnimationController:

MonoBehaviour{

publicintframeNumber=3;

publicbooldestroy=false;

privateintindex=0;

privateintmyIndex=0;

privatefloatframeRate=0;

privatefloatmyTime=0;

//Usethisforinitialization

voidStart(){

frameRate=1.0f/frameNumber;}//Updateiscalledonceperframe

voidUpdate(){

myTime+=Time.deltaTime;

myIndex=(int)(myTime*frameNumber);

index=myIndex%frameNumber;

render.material.mainTextureScale=newVector2(frameRate,1);

render.material.mainTextureOffset=newVector2(index*frameRate,0);

if(index==frameNumber-1&&destroy)

Destroy(gameObject);}}

usingUnityEngine;

usingSystem.Collections;

publicclassBombController:

MonoBehaviour{

publicAudioClipbombSound;//定义炸弹爆炸声音

publicGameObjectexplosion;//定义爆炸动画

privateGameObjectmyTank;//定义被飞机炸弹击中的坦克变量//Usethisforinitialization

voidStart(){

AudioSource.PlayClipAtPoint(bombSound,newVector3(0,1,-5));//播放炸弹的呼啸声音}

//Updateiscalledonceperframe

voidUpdate(){}voidOnTriggerEnter(Colliderother)//碰撞检测{if(other.tag=="ground")//检测炸弹与地面的碰撞{Instantiate(explosion,new

Vector3(transform.position.x,transform.position.y+1.5f,-1f),Quaternion.identity);

//若与地面碰撞调用爆炸动画Destroy(gameObject);//销毁炸弹对象}}

voidOnCollisionEnter(Collisioncollison)//碰撞检测{if(collison.gameObject.tag=="tank0")//检测炸弹与坦克的碰撞{Instantiate(explosion,new

Vector3(transform.position.x,transform.position.y,-1),Quaternion.identity);//调用爆炸动画Destroy(gameObject);//销毁炸弹

Debug.Log("Destroy(gameObject)1;");

myTank=GameObject.FindWithTag("tank0");

//通过GameObject.FindWithTag()来获得被飞机炸弹击中的坦克myTank.transform.position=new

Vector3(5.3f,myTank.transform.position.y,myTank.transform.position.z);

//重置坦克的位置,即重新生成坦克}if(collison.gameObject.tag=="projectile")

//碰撞检测,检测炸弹是否与坦克的子弹发生碰撞,若是则销毁坦克的子弹Destroy(collison.gameObject);//销毁坦克的子弹Debug.Log("Destroy(gameObject)2;");}}

usingUnityEngine;

usingSystem.Collections;

publicclassPlaneController:

MonoBehaviour{

publicfloatspeed=2.0f;//定义飞机的速度

publicRigidbodybomb;//声明Rigidbody类型的,关联炸弹预制件bomb变量privateRigidbodymyBomb;//声明Rigidbody类型的,被发射的炸弹。

见下面代码含义

//Usethisforinitialization

voidStart(){}//Updateiscalledonceperframe

voidUpdate(){

if(Input.GetAxis("Horizontal")>0)//判断获得的horizontal的值是否大于0transform.Translate(Vector3.right*speed*Time.deltaTime);//是,控制飞机向右移动if(Input.GetAxis("Horizontal")<0)//判断获得的horizontal的值是否小于0transform.Translate(2*Vector3.left*speed*Time.deltaTime);//是,飞机向左移动transf

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 教育学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1