Unity3D三款游戏完整(入门进阶必备)代码.doc_第1页
Unity3D三款游戏完整(入门进阶必备)代码.doc_第2页
Unity3D三款游戏完整(入门进阶必备)代码.doc_第3页
Unity3D三款游戏完整(入门进阶必备)代码.doc_第4页
Unity3D三款游戏完整(入门进阶必备)代码.doc_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一 太空陨石大战/*控制太空背景向下移动的游戏脚本*using UnityEngine;using System.Collections;public class BackgroundContoller : MonoBehaviour public float speed=1.0f; /设置背景向下移动的速度/ Use this for initializationvoid Start () / Update is called once per framevoid Update () transform.Translate(0,-speed*Time.deltaTime,0); /实现背景向下移动if(transform.position.y2.14f) /判断子弹位置是否移出游戏屏幕Destroy (gameObject); /若移出,销毁子弹对象/*控制陨石的游戏脚本*using UnityEngine;using System.Collections;public class RockController : MonoBehaviour public float speed=2.0f; /定义陨石的下落速度public GameObject explosionPlayer; /定义爆炸动画对象public GameObject explosionEnemy; /定义爆炸动画对象public static int score=0; /定义击落陨石获得的积分public static int lives=3; /定义玩家生命public static int highScore=0; /设置最高分的存储变量/ Use this for initializationvoid Start () highScore=PlayerPrefs.GetInt(HighScore); /调用PlayerPrefs类中GetInt()方法读取保存在本地的变量highScore/ Update is called once per framevoid Update () transform.Translate(0,-speed*Time.deltaTime,0); /实现陨石在Y方向上的下落运动if(transform.position.yPlayerPrefs.GetInt (HighScore) /若当前获得分数大于之前本地保存的最高分highScore=RockController.score ; /保存最高分变量PlayerPrefs.SetInt (HighScore,RockController.score); /更新最高分elsehighScore=PlayerPrefs.GetInt(HighScore); /否则继续保存本地最高分Application.LoadLevel(Lose);Instantiate(explosionPlayer,transform.position,transform.rotation); /调用飞机与陨石碰撞的爆炸效果transform.position=new Vector3(Random.Range(-2.2f,2.2f),2.5f,0); /重置陨石的的代码,在指定位置重新生成陨石/Destroy(other.gameObject);/销毁飞机,所以将其注释掉,不销毁飞机;Destroy(gameObject)是销毁陨石的 void OnGUI()GUI.Label(new Rect(10,10,120,120),score:+score.ToString();GUI.Label(new Rect(10,30,60,20),Lives:+lives.ToString();GUI.Label (new Rect(10,50,120,20),highscore:+highScore.ToString (); /显示最高分/*游戏开始场景控制代码*using UnityEngine;using System.Collections;public class StartController : MonoBehaviour private string instructionText=Instruction:nn Press left and right arrow to move.n Press Space to fire.; /定义简单的游戏操作说明public Texture startTexture; /定义关联start场景的变量void OnGUI()GUI.DrawTexture (new Rect(0,0,Screen.width,Screen.height),startTexture); /绘制开始start场景区域,并添加关联的场景GUI.Label(new Rect(10,10,250,200),instructionText); /绘制游戏简介内容if(Input.anyKeyDown)Application.LoadLevel(Level); /按下任意键,加载场景1 Debug.Log (Level);/*个性化游戏倒计时控制代码*using UnityEngine;using System.Collections;public class TimeRemainDisplay : MonoBehaviour public Texture timeNumbers; /定义一个贴图数组,用于存放数字图片0,1,2.9public static int leftTime=100; /定义游戏倒计时时间为100秒,并用于存放游戏剩余时间float myTime; / Use this for initializationvoid Start () / Update is called once per framevoid Update () myTime+=Time.deltaTime; /游戏花费时间的累计,每当累计到1秒时,倒计时leftTime减1秒if(myTime1)leftTime-;myTime=0;if(leftTime=0) /若游戏倒计时结束,则转入玩家胜利的Win场景if(RockController.scorePlayerPrefs.GetInt(HighScore)RockController.highScore=RockController.score;PlayerPrefs.SetInt(HighScore,RockController.score);elseRockController.highScore=PlayerPrefs.GetInt(HighScore); Application.LoadLevel(Win); void OnGUI()for(int i=0;ileftTime.ToString().Length;i+)GUI.DrawTexture(new Rect(300+i*32,20,32,45),timeNumbersSystem.Int32.Parse(leftTime.ToString()i.ToString();using UnityEngine;using System.Collections;public class WinController : MonoBehaviour public Texture winTexture;/ Use this for initializationvoid Start () RockController.score=0;RockController.lives=3;TimeRemainDisplay.leftTime=100;/ Update is called once per framevoid OnGUI () GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),winTexture);if(Input.anyKeyDown)Application.LoadLevel(Level);二 坦克大战游戏:using UnityEngine;using System.Collections;public class BackgroundController : MonoBehaviour public float speed=2.0f; /声明背景向左移动的速度private bool change=false; /为背景的循环移动声明一个change变量,用于标识背景是否需要移动/ Use this for initializationvoid Start () / Update is called once per framevoid Update () transform.Translate(Vector3.left*speed*Time.deltaTime);if(transform.position.x-14.4)transform.position=new Vector3(15.5f,transform.position.y,transform.position.z);change=true;if(transform.position.x0) /判断获得的horizontal的值是否大于0transform.Translate(Vector3.right*speed*Time.deltaTime); /是,控制飞机向右移动if(Input.GetAxis(Horizontal)0) /判断飞机是否右移动 myBomb.velocity=new Vector3(-3*speed,0,0); /是,使炸弹获得惯性速度else if(Input.GetAxis(Horizontal)=0) /判断飞机速度为0myBomb.velocity=Vector3.zero; /炸弹右移速度为0elsemyBomb.velocity=new Vector3(3*speed,0,0); /否则飞机左移,使炸弹获得向左的惯性using UnityEngine;using System.Collections;public class ProjectileController : MonoBehaviour public AudioClip explosionSound;public GameObject explosion;/ Use this for initializationvoid Start () AudioSource.PlayClipAtPoint(explosionSound,new Vector3(0,0,-5);/ Update is called once per framevoid Update () if(transform.position.x3.45f)Destroy (gameObject);void OnCollisionEnter(Collision collision)if(collision.gameObject.tag=plane)Instantiate(explosion,new Vector3(transform.position.x,transform.position.y,-1),Quaternion.identity);Destroy (gameObject);using UnityEngine;using System.Collections;public class SoundController : MonoBehaviour public AudioClip explosionSound;/ Use this for initializationvoid Start () AudioSource.PlayClipAtPoint(explosionSound,new Vector3(0,0,-5);/ Update is called once per framevoid Update () using UnityEngine;using System.Collections;public class StartController : MonoBehaviour public Texture2D startTexture;public Texture2D buttonA;public Texture2D buttonB;/ Use this for initializationvoid Start () / Update is called once per framevoid Update()if(Input.GetKeyDown(a)Application.LoadLevel(1);if(Input.GetKeyDown(b)Application.Quit();void OnGUI()GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),startTexture);GUI.Label(new Rect(662,274,100,100),Load Game);GUI.DrawTexture(new Rect(662,294,64,64),buttonA);GUI.Label(new Rect(662,335,200,200),Exit Game);GUI.DrawTexture(new Rect(662,378,64,64),buttonB);GUI.Label(new Rect(619,538,200,200),);using UnityEngine;using System.Collections;public class TankController : MonoBehaviour public float currentSpeed=0;public float maxSpeed=10.0f;public float minSpeed=3.0f;/ Use this for initializationvoid Start () currentSpeed=Random.Range (minSpeed,maxSpeed);/ Update is called once per framevoid Update () transform.Translate(Vector3.left*currentSpeed*Time.deltaTime);if(transform.position.x310)if(isMoving)transform.localEulerAngles=new Vector3(transform.localEulerAngles.x,transform.localEulerAngles.y,transform.localEulerAngles.z-amtToRotate);var fwd=transform.TransformDirection(Vector3.left);if(Physics.Raycast(transform.position,fwd,10) /实现炮台的瞄准功能,一旦10米内发现飞机,则Physics.Raycast(transform.position,fwd,10)条件为真true,则下一行代码发射炮弹myProjectile=Instantiate(projectile,transform.position+fwd,transform.rotation)as Rigidbody; /调用Instantiate()方法发射炮弹myProjectile.velocity=fwd*20; /设置炮弹的发射速度isMoving=false; /跳出旋转炮台的条件语句,置falseStartCoroutine(waitForTime(); /调用延迟函数waitForTime();else /当炮台的旋转角度小于310度时,重新设置炮台的角度为347度transform.localEulerAngles=new Vector3(transform.localEulerAngles.x,transform.localEulerAngles.y,347); /重新设置炮台的角度为347度IEnumerator waitForTime() /被调用的延迟1秒的函数体yield return new WaitForSeconds(1); /通过这里的延迟时间调节坦克发射炮弹的快慢,即每个一秒坦克就发射一次炮弹。isMoving=true; /置true,继续旋转炮台以便瞄准飞机三 平台游戏代码public enum GameState /定义玩家9种游戏状态idle, /静止状态 runLeft, /向左奔跑状态runRight, /向右奔跑状态jumpLeft, /向左跳跃状态jumpRight, /向右跳跃状态idleLeftJump, /静止向左跳跃状态idleRightJump, /静止向右跳跃状态celebrate, /欢呼状态die /死亡状态using UnityEngine;using System.Collections;public class EnemyController : MonoBehaviour private float speed=1.0f;private float startPosition;private bool isRight=true;private AnimationController myAnimationController;/ Use this for initializationvoid Start () myAnimationController=GetComponent();startPosition=transform.position.x;/ Update is called once per framevoid Update () if(isRight)transform.Translate(Vector3.right*speed*Time.deltaTime);myAnimationController.direction=false;elsetransform.Translate(Vector3.left*speed*Time.deltaTime);myAnimationController.direction=true; if(transform.position.x-startPosition)2.0f)isRight=false;using UnityEngine;using System.Collections;public class AnimationController : MonoBehaviour public int frameNumber=3;public bool direction=true;public bool destroy=false;public int lastFrameNo=0;private bool oneTime=true;private int index=0;private float frameRate=0;private float myTime=0;private int myIndex=0;/ Use this for initializationvoid Start () / Update is called once per framevoid Update () frameRate=1.0f/frameNumber;if(oneTime)myTime+=Time.deltaTime;myIndex=(int)(myTime*(frameNumber-1);index=myIndex%frameNumber; if(direction)renderer.material.mainTextureScale=new Vector2(frameRate,1);renderer.material.mainTextureOffset=new Vector2(index*frameRate,0);elserenderer.material.mainTextureScale=new Vector2(-frameRate,1);renderer.material.mainTextur

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论