小鸟能煽动翅膀了,那我们让它飞?别急,几行代码的是,在Bird.cs中添加一下代码
this.rigidbody.velocity = new Vector3(5, 0, 0);//给一个X轴正方向为5的的速度
好了,之后呢,我们该让小鸟在飞翔的过程中的背景无限延伸了,至于这个无限延伸嘛,就是无限重复移动咯,这里我们要用到单例模式:
首先新建GameManager.cs脚本,添加到主摄像机中
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public static GameManager gameInstance;//单例模式用到的,这个不用多说
public enum GameState { GAMESTART = 0, GAMEISPLAYING = 1, GAMEOVER = 2 };//表示游戏执行的状态
public int currentGameState;//记录游戏当前状态
public Transform firstBackGround;//表示背景1,在编辑器中把背景1拖到该变量上来
public int currentScores=0;//记录获得的当前分数
public GameObject scoreTitle;//NGUI做的UI,
private int delayTime = 2;//延迟时间
private GameObject bird;//获取小鸟物体
void Awake()
{
gameInstance = this;//实例化
scoreTitle.SetActive(false);//分数面板在加载场景时设置为不显示
currentGameState = (int)GameState.GAMESTART;//载入“StartGame”场景的时候设置当前游戏状态为此
bird = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
if(currentGameState ==(int)GameState.GAMESTART)
{
if(Input.GetMouseButtonDown(0))
{
scoreTitle.SetActive(true);
GameObject.Find("FlappyBird").SetActive(false);
currentGameState = (int)GameState.GAMEISPLAYING;
bird.SendMessage("GetTheGameBegin");//传递信息到小鸟物体的脚本中
}
}
if(currentGameState==(int)GameState.GAMEOVER) //这是设置游戏结束时的事件,包括记录当前分数和跳转场景,可以之后再回头来看
RemenberScores.currentScore = currentScores;
StartCoroutine("ChangeScene");//协程函数不用解释了
}
}
IEnumerator ChangeScene()
{
yield return new WaitForSeconds(delayTime);//当小鸟响应死亡事件时,我们延迟两秒再跳转场景
Application.LoadLevel("MenuScene");//MenuScene是一个游戏结束时的菜单,后边再做讲解
}
}
好了,GameManager.cs脚本编写完毕。那么问题来了,我们我小鸟能飞了,也能提升了,但是背景在小鸟飞过之后就没了啊,所以,接下来我们来分析以及编写背景“无限平移”的脚本。详见下篇