主要框架視圖
關(guān)鍵代碼
FireWall
public class FireWall : MonoBehaviour {
//火向前推進速度
public float creepSpeed = 0.01f;
//主角引用
private Player player;
//判斷游戲狀態(tài)
private LevelState state;
void Start () {
player = GameObject.FindObjectOfType<Player>();
state = GameObject.FindObjectOfType<LevelState>();
}
void Update () {
//游戲沒結(jié)束
if (!state.isGameOver)
{
FollowPlayer();
CreepFoward();
CheckPlayerBurnt();
}
}
void FollowPlayer() {
//讓火跟隨玩家
GameObject player = GameObject.Find("Player");
Vector3 delta = player.transform.position - transform.position;
Vector3 projectedDelta = Vector3.Project(delta, Vector3.left);
transform.position += projectedDelta;
}
//向前推進速度煌珊;
void CreepFoward() {
transform.position += Vector3.forward * creepSpeed;
}
/// <summary>
/// 檢查玩家是否受傷害
/// </summary>
void CheckPlayerBurnt() {
if (player.transform.position.z<transform.position.z)
{
state.isGameOver = true;
}
}
}
FireDestroyer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireDestroyer : MonoBehaviour {
/// <summary>
/// 銷毀車道
/// </summary>
/// <param name="collider"></param>
private void OnTriggerEnter(Collider collider)
{
//把碰到的車道銷毀掉
Destroy(collider.gameObject);
}
}
GameOverMessage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOverMessage : MonoBehaviour {
//UI距離
public float UIDistance = 5.0f;
public float UIHeight = 2.0f;
//獲取主角的引用
private Player player;
//結(jié)束UI
private Canvas canvas;
//游戲狀態(tài)
private LevelState state;
void Start () {
player = GameObject.FindObjectOfType<Player>();
canvas = GetComponent<Canvas>();
//畫面一開始不可用
canvas.enabled = false;
state = GameObject.FindObjectOfType<LevelState>();
}
void Update () {
//判斷是否游戲結(jié)束
if (state.isGameOver)
{
TrackPlayerHead();
canvas.enabled = true;
}
}
/// <summary>
/// 追蹤主角頭部
/// </summary>
void TrackPlayerHead() {
//結(jié)束UI畫面跟隨主角
//旋轉(zhuǎn)
transform.rotation = Quaternion.LookRotation(player.LookDirection());
//位置
transform.position = player.transform.position;
transform.position += player.LookDirection() * UIDistance;
transform.position += Vector3.up * UIHeight;
}
}
GameState
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameState : MonoBehaviour {
//分數(shù)引用
public int hightScore;
/// <summary>
/// 加載時不要銷毀gamestate 三個場景共用
/// </summary>
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
/// <summary>
/// 更新分數(shù)的方法
/// </summary>
public void UpdateHightScore(int newScore)
{
hightScore = newScore;
}
}
HightScore
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HightScore : MonoBehaviour {
void Start () {
//獲取分數(shù)
GameState state = GameObject.FindObjectOfType<GameState>();
Text text = GetComponent<Text>();
text.text ="High Score: "+ state.hightScore+" Meters";
}
}
LethalCollider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//此腳本掛在車上
public class LethalCollider : MonoBehaviour {
/// <summary>
/// 車碰到玩家及游戲結(jié)束
/// </summary>
/// <param name="collision"></param>
private void OnCollisionEnter(Collision collision)
{
//獲取玩家
Player player = collision.gameObject.GetComponent<Player>();
//判斷玩家是否為空
if (player !=null)
{
LevelState state = GameObject.FindObjectOfType<LevelState>();
state.isGameOver = true;
}
}
}
LevelState
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelState : MonoBehaviour {
//判斷游戲是否結(jié)束的字段
public bool _isGameOver = false;
//判斷游戲是否結(jié)束屬性
public bool isGameOver{
get { return _isGameOver; }
set {
if (value)
{
SetHightScore();
}
_isGameOver = true;
}
}
/// <summary>
/// 設(shè)置分數(shù)
/// </summary>
void SetHightScore() {
GameState state = GameObject.FindObjectOfType<GameState>();
Player player = GameObject.FindObjectOfType<Player>();
//數(shù)學(xué)公式 將位置轉(zhuǎn)成數(shù)值
int score = Mathf.FloorToInt(player.transform.position.z);
state.UpdateHightScore(score);
}
//重新開始游戲
public void ResetGame() {
//SceneManager.LoadScene("Main");
//重構(gòu)代碼
ScenesLoder loader = GameObject.FindObjectOfType<ScenesLoder>();
loader.LoadLevelByOffset(0);
}
//返回游戲
public void BackGame(){
// SceneManager.LoadScene("SphashScreen");
//重構(gòu)代碼
ScenesLoder loader = GameObject.FindObjectOfType<ScenesLoder>();
loader.LoadLevelByOffset(-1);
}
}
Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour {
//獲取視選引用
private CardboardHead Head;
//聲明一個公開的變量 吧UI拖進去
// public Text GazeText;
//添加剛體
private Rigidbody rigi;
//引用角速度和速度
public float jumpAngleIndegree=32f;
public float jumpSpeed=3f;
//檢查是否碰撞到地面
private bool onFloor;
//最后一跳時間
private float lastJumpRequestTime = 0f;
//獲取游戲狀態(tài)
private LevelState state;
void Start()
{
//觸發(fā)事件
Cardboard.SDK.OnTrigger += PullTrigger;
//獲取引用
Head = GameObject.FindObjectOfType<CardboardHead>();
//獲取rigi組件
rigi = GetComponent<Rigidbody>();
state = GameObject.FindObjectOfType<LevelState>();
}
void Update()
{
//在UI上打印視選值践美;
// GazeText.text = GazePrintHead.Gaze.ToString();
}
/// <summary>
/// 觸發(fā)事件后的行為 盒子上的小彈片 點擊事件
/// </summary>
void PullTrigger()
{
//打印測試
// print("Pull The Trigger!!!");
//視選文本取反 點擊隱藏或顯示
//GazeText.enabled = !GazeText.enabled;
//在頭部凝視方向添加一個向前的力
// rigi.AddForce(Head.Gaze.direction*1000);
//改成速率
// rigi.velocity = Head.Gaze.direction * 10;
//是否在地面
//if (onFloor)
//{
//}
RequestJump();
}
//請求是夠可以眺
private void RequestJump() {
lastJumpRequestTime = Time.time;
rigi.WakeUp();
}
//起跳
private void Jump() {
//判斷游戲不是結(jié)束狀態(tài)才可以眺陨倡,結(jié)束不能跳;
if (!state.isGameOver)
{
//Jump
//度轉(zhuǎn)弧度
float jumpAngleInRadins = jumpAngleIndegree * Mathf.Deg2Rad;
// Vector3 projectedVector = Vector3.ProjectOnPlane(Head.Gaze.direction, Vector3.up);
// Vector3 jumpVector = Vector3.RotateTowards(projectedVector, Vector3.up, jumpAngleInRadins, 0);
Vector3 jumpVector = Vector3.RotateTowards(LookDirection(), Vector3.up, jumpAngleInRadins, 0);
rigi.velocity = jumpVector * jumpSpeed;
}
}
public Vector3 LookDirection() {
return Vector3.ProjectOnPlane(Head.Gaze.direction, Vector3.up);
}
//檢測是否碰撞到地面 避免在空中連續(xù)跳
//private void OnCollisionEnter(Collision collision)
//{
// onFloor = true;
//}
//private void OnCollisionExit(Collision collision)
//{
// onFloor = false;
//}
/// <summary>
/// 當(dāng)碰到物體時绎晃,判斷起跳時間
/// </summary>
/// <param name="collision"></param>
private void OnCollisionStay(Collision collision)
{
float delta = Time.time - lastJumpRequestTime;
if (delta<0.1f)
{
Jump();
lastJumpRequestTime = 0f;
}
}
}
ScenesLoder
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ScenesLoder : MonoBehaviour {
//延遲調(diào)用時間 開始啟動
public float loadLevelAfterTime = 0;
void Start () {
//判斷時間大于零的話 調(diào)用場景
if (loadLevelAfterTime>0)
{
Invoke("LoadNextLevel", loadLevelAfterTime);
}
}
//調(diào)用場景的方法
public void LoadNextLevel() {
//調(diào)用指定場景
// SceneManager.LoadScene("Login");
//當(dāng)前場景直接調(diào)轉(zhuǎn)下一場景
// SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);
LoadLevelByOffset(1);
}
//迭代
public void LoadLevelByOffset(int indexOffset) {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + indexOffset);
}
}
Spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
//隨機生成有草馬路或者普通馬路
public GameObject[] lanePrefabs;
//生成的馬路長度
public float spawnHorizon = 500f;
//車道的寬度
public float laneWidth = 2f;
//主角
public GameObject player;
//父類庶艾;
public Transform spwanerParent;
//偏移
private float nextLaneOffset = 0f;
void Start () {
}
void Update () {
//主角的位置
float forwardPosition = player.transform.position.z;
//判斷主角位置+長度大于偏移位置 實例化物體
while (forwardPosition+spawnHorizon>nextLaneOffset)
{
//隨機馬路形式的范圍
int randomLaneIndex = Random.Range(0,lanePrefabs.Length);
//隨機的預(yù)制體
GameObject lane = lanePrefabs[randomLaneIndex];
//定義下一個馬路的位置
Vector3 nextLanePosition = nextLaneOffset * Vector3.forward;
//實例化馬路
GameObject newLane= Instantiate(lane,nextLanePosition,Quaternion.identity) as GameObject;
//父類
newLane.transform.parent = spwanerParent;
//偏移量
nextLaneOffset += laneWidth;
}
}
}
Vehicle
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//此腳本掛在車上
public class Vehicle : MonoBehaviour {
//定義速度
private float speed=5.0f;
//定義銷毀長度
// private float lifeTime = 10f;
private float length = 20f;
void Start () {
//函數(shù)調(diào)用 10秒過后移除車輛
// Invoke("RemoveVehicle",lifeTime);
float lifeTime = length / speed;
Invoke("RemoveVehicle",lifeTime);
}
void Update () {
//車跑起來了
transform.position += Vector3.right * speed * Time.deltaTime;
}
//速度可改變
public void SetPath(float someSpeed,float someLifeTime) {
speed = someSpeed;
length = someLifeTime;
}
/// <summary>
///移除車輛
/// </summary>
public void RemoveVehicle() {
Destroy(gameObject);
}
}
VehicleSpanwer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VehicleSpanwer : MonoBehaviour {
//此腳本掛在預(yù)制體道路上
//定義公開的車輛
public GameObject[] vehiclePrefabs;
//高度
public float heightOffset = 1f;
//開始位置
public float StartOffset = -20f;
//速度
public float Speed=5.0f;
//車輛銷毀時間
// public float LifeTime = 2f;
public float length = 20f;
//生成車輛時間
public float maxSpwanTime=5f;
void Start() {
//test
//Instantiate(vehiclePrefabs[0],transform.position,transform.rotation);
// InstantiateVehicle();
//開啟協(xié)程
StartCoroutine("Spawn");
}
/// <summary>
/// 實例化車輛
/// </summary>
public void InstantiateVehicle(int vehicleIndex) {
GameObject vehicleObject = Instantiate(vehiclePrefabs[vehicleIndex]) ;
vehicleObject.transform.position = GetPositionOffset();
vehicleObject.transform.parent = transform;
//獲取車輛
Vehicle vehicleComponent =vehicleObject.GetComponent<Vehicle>();
//設(shè)置速度
vehicleComponent.SetPath(Speed,length);
}
/// <summary>
/// 獲取相應(yīng)位置
/// </summary>
/// <returns></returns>
Vector3 GetPositionOffset(){
//方向和位置;
Vector3 positionOffset = transform.position;
//車向上位置
positionOffset += heightOffset * Vector3.up*0.95f ;
//車左邊位置
positionOffset += StartOffset * Vector3.right;
return positionOffset;
}
/// <summary>
/// 協(xié)程
/// </summary>
/// <returns></returns>
IEnumerator Spawn() {
while (true)
{
// int vehicleIndex = Random.Range(0,vehiclePrefabs.Length); ;
// InstantiateVehicle(vehicleIndex);
// yield return new WaitForSeconds(5.0f);
//隨機時間
WaitForSeconds radomWait = new WaitForSeconds(Random.Range(0.5f,maxSpwanTime));
yield return radomWait;
int vehicleIndex = Random.Range(0,vehiclePrefabs.Length); ;
InstantiateVehicle(vehicleIndex);
}
}
void Update () {
}
}