资源描述
一. 太空陨石大战
//******控制太空背景向下移动的游戏脚本******
using UnityEngine;
using System.Collections;
public class BackgroundContoller : MonoBehaviour {
public float speed=1.0f; //设置背景向下移动的速度
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(0,-speed*Time.deltaTime,0); //实现背景向下移动
if(transform.position.y<-5.17f) //判断背景是否移出主摄像机照射区域(即游戏屏幕)
transform.position=new Vector3(0,12,1); //若移出,则重新设置游戏背景Y轴位置如0,6,12,
//以便实现无缝连接使游戏背景连续播放
}
}
using UnityEngine;
using System.Collections;
public class ExplosionController : MonoBehaviour {
public int index=0;
public int frameNumber=7; //定义帧数,爆炸动画由7帧组成
float frameRate=0; //定义帧速率
float myTime=0;
int myIndex=0;
// Use this for initialization
void Start () {
frameRate=1.0f/frameNumber; //帧速率=0.143=1/7,帧数framenumber=7
}
// Update is called once per frame
void Update () {
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);
renderer.material.mainTextureScale=new Vector2(frameRate,1);
//通过renderer.material.mainTextureScale设置tiling帧宽属性
renderer.material.mainTextureOffset=new Vector2(index*frameRate,0);
//通过renderer.material.mainTextureOffset设置帧偏移量
if(index==frameNumber-1) //如果动画帧数播放完等于6,即索引等于6,则销毁动画
Destroy (gameObject);
}
}
using UnityEngine;
using System.Collections;
public class LoseController : MonoBehaviour {
public Texture loseTexture;
// Use this for initialization
void Start () {
RockController.score=0;
RockController.lives=3;
TimeRemainDisplay.leftTime=100;
}
// Update is called once per frame
void OnGUI () {
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),loseTexture);
if(Input.anyKeyDown)
{
Application.LoadLevel("Level");
}
}
}
//****控制飞机的游戏脚本****
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed=2.0f; //定义飞机在X轴上的移动速度
public GameObject projectile; //定义一个共有子弹对象
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(speed*Input.GetAxis("Horizontal")*Time.deltaTime,0,0); //实现飞机在X轴上,按下左右键时可水平移动,speed一定要乘上
if(Input.GetKeyDown(KeyCode.Space)) //判断是否按下空格键
Instantiate(projectile,transform.position,transform.rotation); //按下空格键时,发射炮弹对象projectile,炮弹发射位置飞机中部transform。position
} //炮弹发射角度transform。rotation
}
//******控制子弹的游戏脚本******
using UnityEngine;
using System.Collections;
public class ProjectileController : MonoBehaviour {
public float speed=5.0f; //定义炮弹发射速度
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (0,speed*Time.deltaTime,0); //实现子弹在屏幕上发射时,在Y轴上移动
if(transform.position.y>2.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 initialization
void Start () {
highScore=PlayerPrefs.GetInt("HighScore"); //调用PlayerPrefs类中GetInt()方法读取保存在本地的变量highScore
}
// Update is called once per frame
void Update () {
transform.Translate(0,-speed*Time.deltaTime,0); //实现陨石在Y方向上的下落运动
if(transform.position.y<-2.56f) //判断陨石在Y轴向的位置,是否离开游戏界面下边界
transform.position=new Vector3(Random.Range(-2.2f,2.2f),2.5f,0); //若离开则在上界面随机重新生成陨石
}
void OnTriggerEnter (Collider other) //碰撞检测
{
if(other.tag=="projectile") //子弹与陨石的碰撞检测,判断碰撞的标签是否为子弹
{
score+=100;
Debug.Log("score+=100;");
Instantiate(explosionEnemy,transform.position,transform.rotation);
//调用炮弹碰撞陨石所发生的爆炸效果
transform.position=new Vector3(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=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:\n\n 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...9
public static int leftTime=100; //定义游戏倒计时时间为100秒,并用于存放游戏剩余时间
float myTime;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
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");
}
}
void OnGUI()
{
for(int i=0;i<leftTime.ToString().Length;i++)
GUI.DrawTexture(new Rect(300+i*32,20,32,45),timeNumbers[System.Int32.Parse(leftTime.ToString()[i].ToString())]);
}
}
using UnityEngine;
using System.Collections;
public class WinController : MonoBehaviour {
public Texture winTexture;
// Use this for initialization
void Start () {
RockController.score=0;
RockController.lives=3;
TimeRemainDisplay.leftTime=100;
}
// Update is called once per frame
void 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 initialization
void Start () {
}
// Update is called once per frame
void 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.x<0.5 && change)
{
transform.position=new Vector3(0.5f,transform.position.y,transform.position.z);
change=false;
}
}
}
using UnityEngine;
using System.Collections;
public class AnimationController : MonoBehaviour {
public int frameNumber=3;
public bool destroy=false;
private int index=0;
private int myIndex=0;
private float frameRate=0;
private float myTime=0;
// Use this for initialization
void Start () {
frameRate=1.0f/frameNumber;
}
// Update is called once per frame
void Update () {
myTime+=Time.deltaTime;
myIndex=(int)(myTime*frameNumber);
index=myIndex%frameNumber;
renderer.material.mainTextureScale=new Vector2(frameRate,1);
renderer.material.mainTextureOffset=new Vector2(index*frameRate,0);
if(index==frameNumber-1 && destroy)
Destroy (gameObject);
}
}
using UnityEngine;
using System.Collections;
public class BombController : MonoBehaviour {
public AudioClip bombSound; //定义炸弹爆炸声音
public GameObject explosion; //定义爆炸动画
private GameObject myTank; //定义被飞机炸弹击中的坦克变量
// Use this for initialization
void Start () {
AudioSource.PlayClipAtPoint(bombSound,new Vector3(0,1,-5)); //播放炸弹的呼啸声音
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other) //碰撞检测
{
if(other.tag=="ground") //检测炸弹与地面的碰撞
{
Instantiate(explosion,new Vector3(transform.position.x,transform.position.y+1.5f,-1f),Quaternion.identity);
//若与地面碰撞调用爆炸动画
Destroy (gameObject); //销毁炸弹对象
}
}
void OnCollisionEnter(Collision collison) //碰撞检测
{
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;");
}
}
using UnityEngine;
using System.Collections;
public class PlaneController : MonoBehaviour {
public float speed=2.0f; //定义飞机的速度
public Rigidbody bomb; //声明Rigidbody类型的,关联炸弹预制件bomb变量
private Rigidbody myBomb; //声明Rigidbody类型的,被发射的炸弹。见下面代码含义
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetAxis("Horizontal")>0) //判断获得的horizontal的值是否大于0
transform.Translate(Vector3.right*speed*Time.deltaTime); //是,控制飞机向右移动
if(Input.GetAxis("Horizontal")<0) //判断获得的horizontal的值是否小于0
transform.Translate(2*Vector3.left*speed*Time.deltaTime); //是,飞机向左移动
transform.Translate(Input.GetAxis("Vertical")*Vector3.up*Time.deltaTime*speed);
//控制飞机上下移动
if(Input.GetButtonDown("Jump")) //判断是否按下空格键,按下为true
{
myBomb=Instantiate(bomb,transform.position+new Vector3(-4.357f,1.631f,0),transform.rotation) as Rigidbody;
//将Object类强制转换类型为Rigidbody类
//new Vector3(-4.357f,1.931f,0):处于飞机刚体内的一点,此位置也被刚体包裹着,若此时发射的刚体炮弹处于这个刚体内,则炮弹不会受重力影响下落
//致使else if(Input.GetAxis("Horizontal")==0) myBomb.velocity=Vector3.zero;(此时飞机静止)没有炸弹下落的效果。而必须调节这个参数如上。
//总结:一个目标刚体对象连续调用生成其他克隆对象,克隆对象刚体一定要在目标对象的刚体外,如上的飞机与炸弹。
if(Input.GetAxis("Horizontal")>0) //判断飞机是否右移动
myBomb.velocity=new Vector3(-3*speed,0,0); //是,使炸弹获得惯性速度
else if(Input.GetAxis("Horizontal")==0) //判断飞机速度为0
myBomb.velocity=Vector3.zero; //炸弹右移速度为0
else
myBomb.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 initialization
void Start () {
AudioSource.PlayClipAtPoint(explosionSound,new Vector3(0,0,-5));
}
// Update is called once per frame
void Update () {
if(transform.position.x<-8.302f || transform.position.y>3.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 initialization
void Start () {
AudioSource.PlayClipAtPoint(explosionSound,new Vector3(0,0,-5));
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class StartController : MonoBehaviour {
public Texture2D startTexture;
public Texture2D buttonA;
public Texture2D buttonB;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void 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),"longguantianxia@");
}
}
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 initialization
void Start () {
currentSpeed=Random.Range (minSpeed,maxSpeed);
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.left*currentSpeed*Time.deltaTime);
if(transform.position.x<-8.302f)
{
tran
展开阅读全文