using UnityEngine;using System.Collections;////// 背景滚动/// public class Done_BGScroller : MonoBehaviour{ ////// 滚动速度 /// public float scrollSpeed; ////// z轴的长度 /// public float tileSizeZ; ////// 背景起始位置 /// private Vector3 startPosition; void Start () { //缓存起始位置 startPosition = transform.position; } void Update () { float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ); transform.position = startPosition + Vector3.forward * newPosition; }}
using UnityEngine;using System.Collections;////// 被边界摧毁/// public class Done_DestroyByBoundary : MonoBehaviour{ ////// 敌人,陨石等离开边界被摧毁 /// void OnTriggerExit (Collider other) { Destroy(other.gameObject); }}
using UnityEngine;using System.Collections;////// 主角被敌对物碰到后销毁/// public class Done_DestroyByContact : MonoBehaviour{ ////// 爆炸特效 /// public GameObject explosion; ////// 玩家爆炸特效 /// public GameObject playerExplosion; ////// 玩家得分 /// public int scoreValue; ////// 游戏控制器 /// private Done_GameController gameController; void Start () { GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent(); } if (gameController == null) { Debug.Log ("Cannot find 'GameController' script"); } } /// /// 碰到触发器 /// /// void OnTriggerEnter (Collider other) { //如果是边界或敌人则不处理 if (other.tag == "Boundary" || other.tag == "Enemy") { return; } if (explosion != null) { Instantiate(explosion, transform.position, transform.rotation); } //如果碰到玩家,实例化玩家爆炸特效 //游戏结束 if (other.tag == "Player") { Instantiate(playerExplosion, other.transform.position, other.transform.rotation); gameController.GameOver(); } // gameController.AddScore(scoreValue); //删除碰到的游戏物体 Destroy (other.gameObject); //删除自身游戏物体 Destroy (gameObject); }}
using UnityEngine;using System.Collections;////// 特效经过一段时间后销毁/// public class Done_DestroyByTime : MonoBehaviour{ ////// 生存周期 /// public float lifetime; void Start () { Destroy (gameObject, lifetime); }}
using UnityEngine;using System.Collections;////// 敌机的躲避策略/// public class Done_EvasiveManeuver : MonoBehaviour{ ////// 边界 /// public Done_Boundary boundary; ////// 倾斜 /// public float tilt; ////// 闪避 /// public float dodge; public float smoothing; ////// /// public Vector2 startWait; ////// /// public Vector2 maneuverTime; ////// /// public Vector2 maneuverWait; private float currentSpeed; private float targetManeuver; void Start () { currentSpeed = GetComponent().velocity.z; StartCoroutine(Evade()); } /// /// 躲避协程 /// ///IEnumerator Evade () { yield return new WaitForSeconds (Random.Range (startWait.x, startWait.y)); while (true) { targetManeuver = Random.Range (1, dodge) * -Mathf.Sign (transform.position.x); yield return new WaitForSeconds (Random.Range (maneuverTime.x, maneuverTime.y)); targetManeuver = 0; yield return new WaitForSeconds (Random.Range (maneuverWait.x, maneuverWait.y)); } } void FixedUpdate () { float newManeuver = Mathf.MoveTowards (GetComponent ().velocity.x, targetManeuver, smoothing * Time.deltaTime); GetComponent ().velocity = new Vector3 (newManeuver, 0.0f, currentSpeed); GetComponent ().position = new Vector3 ( Mathf.Clamp(GetComponent ().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp(GetComponent ().position.z, boundary.zMin, boundary.zMax) ); GetComponent ().rotation = Quaternion.Euler (0, 0, GetComponent ().velocity.x * -tilt); }}
using UnityEngine;using System.Collections;////// 游戏控制/// public class Done_GameController : MonoBehaviour{ ////// 敌人预设列表 /// public GameObject[] hazards; ////// 敌人孵化的范围 /// public Vector3 spawnValues; ////// 每波敌人的数量 /// public int hazardCount; ////// 生成每个敌人后的等待时间 /// public float spawnWait; ////// 第一次孵化敌人的等待时间 /// public float startWait; ////// 生成每波敌人后的生成时间 /// public float waveWait; ////// 分数文本 /// public GUIText scoreText; ////// 重新开始文本 /// public GUIText restartText; ////// 游戏结束文本 /// public GUIText gameOverText; ////// 游戏是否结束 /// private bool gameOver; ////// 是否重新开始 /// private bool restart; ////// 得分 /// private int score; void Start () { gameOver = false; restart = false; restartText.text = ""; gameOverText.text = ""; score = 0; UpdateScore (); StartCoroutine (SpawnWaves ()); } void Update () { if (restart) { if (Input.GetKeyDown (KeyCode.R)) { Application.LoadLevel (Application.loadedLevel); } } } ////// 孵化波 /// ///IEnumerator SpawnWaves () { //等待 yield return new WaitForSeconds (startWait); //无限生成敌人 while (true) { //生成敌人 for (int i = 0; i < hazardCount; i++) { GameObject hazard = hazards [Random.Range (0, hazards.Length)]; Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate (hazard, spawnPosition, spawnRotation); yield return new WaitForSeconds (spawnWait); } //生成每波敌人后的等待时间 yield return new WaitForSeconds (waveWait); if (gameOver) { restartText.text = "Press 'R' for Restart"; restart = true; break; } } } /// /// 添加分数 /// /// public void AddScore (int newScoreValue) { score += newScoreValue; UpdateScore (); } ////// 更新分数显示文本 /// void UpdateScore () { scoreText.text = "Score: " + score; } ////// 游戏结束 /// public void GameOver () { gameOverText.text = "Game Over!"; gameOver = true; }}
using UnityEngine;using System.Collections;////// 移动类/// public class Done_Mover : MonoBehaviour{ ////// 移动速度 /// public float speed; void Start () { //给刚体设置一个初始速度 GetComponent().velocity = transform.forward * speed; }}
using UnityEngine;using System.Collections;////// 边界/// [System.Serializable]public class Done_Boundary { public float xMin, xMax, zMin, zMax;}////// 玩家控制/// public class Done_PlayerController : MonoBehaviour{ ////// 移动速度 /// public float speed; ////// 倾斜 /// public float tilt; ////// 游戏边界 /// public Done_Boundary boundary; ////// 子弹预设 /// public GameObject shot; ////// 子弹射出的位置 /// public Transform shotSpawn; ////// 射击频率,每秒发射子弹数 /// public float fireRate; ////// 下一次子弹发射的时间 /// private float nextFire; void Update () { //按下发射,且当前时间大于下一次子弹发射的时间 if (Input.GetButton("Fire1") && Time.time > nextFire) { //重新设置下一次发射子弹的时间 nextFire = Time.time + fireRate; Instantiate(shot, shotSpawn.position, shotSpawn.rotation); GetComponent().Play (); } } void FixedUpdate () { //获取水平和垂直方向的输入值,将其应用在刚体的速度上 float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); GetComponent ().velocity = movement * speed; //限制刚体在边界内移动 GetComponent ().position = new Vector3 ( Mathf.Clamp (GetComponent ().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (GetComponent ().position.z, boundary.zMin, boundary.zMax) ); //设置飞机在z轴的倾斜 GetComponent ().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent ().velocity.x * -tilt); }}
using UnityEngine;using System.Collections;////// 陨石随机移动/// public class Done_RandomRotator : MonoBehaviour { ////// 翻滚速度 /// public float tumble; void Start () { //设置陨石的角速度 GetComponent().angularVelocity = Random.insideUnitSphere * tumble; }}
using UnityEngine;using System.Collections;////// 敌人的武器控制/// public class Done_WeaponController : MonoBehaviour{ ////// 子弹预设 /// public GameObject shot; ////// 发射子弹的位置 /// public Transform shotSpawn; ////// 发射子弹频率, /// public float fireRate; ////// 发射子弹延迟 /// public float delay; void Start () { InvokeRepeating ("Fire", delay, fireRate); } void Fire () { Instantiate(shot, shotSpawn.position, shotSpawn.rotation); GetComponent().Play(); }}
游戏视频:https://pan.baidu.com/s/1o87Anwe
游戏项目:https://pan.baidu.com/s/1c246OQs